Querying and composite keys
Tagged as LightSpeedIn 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
Categories
BrainDump (1)
Community Code (4)
Events (16)
F# (14)
General (53)
Lab Samples (2)
LightSpeed (268)
MegaPack (8)
News (71)
NHibernate Designer (26)
Nightly news (53)
Phone Elements (24)
Products (87)
Projects (5)
Screencast (6)
SharePoint (3)
Silverlight (14)
Silverlight Elements (66)
SimpleDB Management Tools (20)
Visual Studio (9)
VS File Explorer (7)
Web Workbench (39)
WPF (44)
WPF Diagrams (57)
WPF Elements (110)
WPF Property Grid (32)



Posted by Ivan Towlson on 4 January 2010 


