Home » Blog

rounded header

Querying and composite keys

tag icon Tagged as LightSpeed

In a previous episode, we saw how to define and create entities with composite keys in LightSpeed 3. It’s just barely possible you’ll want to get these entities back from the database at some point. In most cases, you’ll just do a Find or a LINQ query on the attributes you want to query on, just as you would with any other entity. But what if you want to query on the composite key itself?

Selecting an entity by ID

If you know the ID — or the ID components — of the entity you want to query for, you can get that entity using the familiar FindById (formerly known as FindOne) API:

ProductId id = new ProductId(2, "fr-CA");
Product product = _unitOfWork.FindById<Product>(id);

Selecting by partial ID

If you know part of the ID, and want to get all the entities that match that partial ID, you can use dot notation to specify that sub-attribute of the Id property. For example, suppose we want to find all Products for the French Canadian market:

var frenchCanadianProducts = _unitOfWork.Find<Product>(
  Entity.Attribute("Id.CultureId") == "fr-CA");

And of course you can combine this with other attribute queries or with other parts of the composite key:

var nonFrenchCanadianWidgets = _unitOfWork.Find<Product>(
  Entity.Attribute("Id.ProductTypeId") == widgetTypeId
    && Entity.Attribute("Id.CultureId") != "fr-CA");

That’s about it for things you need to know about querying composite-keyed entities. Next we’ll look at how associations work in the presence of composite keys.

Leave a Reply

Data Products Visual Controls Community Store
LightSpeed ORM
NHibernate Designer
SimpleDB Tools
SharePoint Tools
WPF Elements
WPF Diagrams
Silverlight Elements
Forums
Blog
Register
Login
Subscribe to newsletter
Buy Now
My Account
Volume Discounts
Purchase Orders
Contact Us