Entity Framework: returning the Entity with the Min() cost

Entity Framework: returning the Entity with the Min() cost

Post by TWFya2x » Sat, 05 Sep 2009 10:59:01


Hi,

I'm trying to find an efficient way to search for entity(s) with the lowest
cost from a table of about 2,000 items. Rather than loading all 2,000 items,
which I don't otherwise need, I'm wanting to use an ObjectQuery to return
just the one(s) I need.

I can see a 2-step way of doing this, but want to do it in just one call.

Assuming a table called 'Items', with fields ID, Name, Type and Cost:

float minCost = objectContext.Items.Where(i=>i.Type ==
"SmallType").Min(i=>i.Cost).FirstOrDefault();

ObjectQuery<Item> items = objectContext.Items.Where(z=>z.Cost == minCost);

Is there a way to do this which results in just one call to the database?

Thanks.
+M