Lets say I have a entity model with three entities “Item”, “ItemType” and “ItemPart”. I’m trying to write a query that will pull the most recent item (by item.date) for each item type and include the parts in the query so I can use them after I get the results. I wrote a query that looks like this:
IQueryable<Item> results = context.Items
.Include("ItemTypes")
.Include("ItemParts")
.Where(x => x.CurrentItem == true)
.GroupBy(x => x.ItemType).Select(group => group.OrderByDescending(item => item.date).FirstOrDefault());
If I run it without the GroupBy line everything works and I can get the included items. If I add the GroupBy line the included objects are null. What am I doing wrong?
EDIT: did some more testing. It seems that it’s actually the select that’s breaking the includes. I guess they don’t get carried through the select. Now to find a way to build the query that will work.
I’m still not positive what was going on but somehow the select or group was breaking the includes I had. Anyway I just did a manual select by the group and got the most recent one then selected it into a new list.