2

I retrieve a collection with the following query:

var numbers = _betDetailItem.GetBetDetailItems().Where(betDetailItem => betDetailItem.BetDetail.Bet.DateDrawing == resultToCreate.Date && betDetailItem.BetDetail.Bet.Status == 1).Where(condition);

Right there I'm able to access my navigation properties and navigate through binded info. Note how I actually use them to filter the data.

After I group the results, the navigation properties become null.

var grouped = numbers.GroupBy(p => p.BetDetail.Bet);
//Iterate through the collection created by the Grouping
foreach (IGrouping<Bet, BetDetailItem> group in grouped)
{
    var details = group.Key.BetDetails; //This is what doesn't work. BetDetails is a navigation property which was accessible in the previous query. 
}

Am I doing something wrong?

6
  • They don't. Something else is going on here which isn't in your post. Try a simple query against Northwind without all your custom code. If you can't get that to work, post it here. Feb 11, 2010 at 14:22
  • 1
    Why can't this work like every other ORM?
    – Raúl Roa
    Feb 11, 2010 at 18:15
  • As I said, I think your question is based on a false premise. Did you try what I suggested? Feb 11, 2010 at 22:44
  • I did a GroupBy against NorthWind on LinqPad, and got the result I expected. The problem is not the grouping. The problem are the navigation properties of the grouped elements.
    – Raúl Roa
    Feb 12, 2010 at 11:40
  • 1
    If it works on Northwind, but not your model, I think we need to see more of the code that you're talking about -- what properties you can access on 'numbers', what you mean by "binded info" and what you get after grouping.
    – Jay
    Feb 12, 2010 at 12:33

2 Answers 2

1

You are confusing LINQ to Entities and object operations.

This is LINQ to Entities:

var numbers = _betDetailItem.GetBetDetailItems().Where(betDetailItem => betDetailItem.BetDetail.Bet.DateDrawing == resultToCreate.Date && betDetailItem.BetDetail.Bet.Status == 1).Where(condition);

So is this:

var grouped = numbers.GroupBy(p => p.BetDetail.Bet);

These are object operations:

foreach (IGrouping<Bet, BetDetailItem> group in grouped)
{
    var details = group.Key.BetDetails; //This is what doesn't work. BetDetails is a navigation property which was accessible in the previous query. 
}

In LINQ to Entities, there is never any need to think about loading related instances. You can always refer to any property of any object. However, at some point, you want to move out of the LINQ to Entities world and into object space, because you want to work with instances of type BetDetail instead of type IQueryable<BetDetail>. This means that the Entity Framework is now required to generate SQL to retrieve data from the database. At that point, it doesn't snow which related instances you will be accessing in your code later on. Nothing in your LINQ to Entities query forces the loading of the related Bet. So unless you do something to cause it to be loaded, like use eager loading, explicit loading, or EF 4 lazy loading, it won't be loaded.

Using lazy loading (e.g., in Entity Framework 4, or in another ORM) will make this code appear to function, but it will be unnecessarily slow, due to the large number of database queries generated. A better solution would be to use eager loading or projection. This way there will be only one DB roundtrip.

1
  • Links to eager loading and projection are stale. Jan 15, 2020 at 18:20
1

Once you do a GroupBy(), you're no longer dealing with your entities -- they have been... well, grouped, so the var in var grouped = ... is now of type IEnumerable<IGrouping<.... As such, the methods available on the items in the grouped collection are the methods of the IGrouping<> interface.

You may want to OrderBy() instead of GroupBy(), depending on your need, or you'll need to iterate on two levels: iterate over each group in grouped, and over each member within each of those.

Once you are inside of a particular IGrouping<>, you should have access to the properties for which you are looking.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.