0

An expansion on the question asked here, I am trying to do the same functionality, namely

IEnumerable<Customer> filteredList = originalList
  .GroupBy(customer => customer.CustomerId)
  .Select(group => group.First());

which in the comments, it was implied that this will not work if it is Linq to Entities and certainly isn't working for me, I get a null result set.

Doesn't work for me ! Even I tried 'FirstOrDefault' it didn't work. – JatSing Sep 25 '11 at 13:55

@JatSing: You must be using Linq to Entities. – Josh Mouch Feb 14 '12 at 14:45

So if the above doesn't work for Linq to Entities, how would I go about grouping an IEnumerable of an EntityType message set by two columns, and then get the first row of each group?

Community
  • 1
  • 1
ItinerantEngineer
  • 255
  • 1
  • 4
  • 14
  • By "null result set," do you mean that filteredList is an IEnumerable with no values in it, or are you saying that `filteredList` is literally `null`? If it's the former, are you sure that `originalList` has any contents? – UtopiaLtd Nov 02 '16 at 18:46
  • An IEnumerable with no values, the originalList has the expected contents – ItinerantEngineer Nov 02 '16 at 18:47
  • 1
    Probably not ideal, but what if you called a `.ToList()` before the `.GroupBy(...)`? – UtopiaLtd Nov 02 '16 at 18:48
  • 3
    The linked post is quite old. If `originalList` is a EF `IQueryable`, then just changing `First` to `FirstOrDefault` will work with the latest EF6.1.3. – Ivan Stoev Nov 02 '16 at 18:52
  • That.... actually works UtopiaLtd. Please post it up as an answer! And to Ivan, sadly this code is stuck back in EF4-land but an upvote for those who read this and can use the newest stuff – ItinerantEngineer Nov 02 '16 at 21:34

1 Answers1

0

If you're having issues with how LINQ expressions are being evaluated via Entity Framework, etc., you can call .ToList() to force the query to be realized into memory for further processing. This is less than ideal if your results from the database or other source are very expensive to fetch and can be narrowed further first.

IEnumerable<Customer> filteredList = originalList
  .ToList()
  .GroupBy(customer => customer.CustomerId)
  .Select(group => group.First());

The .ToList() call makes Entity Framework load your full originalList from the database (which I assume is an IQueryable<T> of some sort), allowing .GroupBy(...) to be evaluated in memory off of the full results of originalList rather than being part of the SQL query EF builds.

UtopiaLtd
  • 2,520
  • 1
  • 30
  • 46
  • `.FirstOrDefault()` as mentioned in a comment above is a better approach when you're working with a newer version of Entity Framework, but `.First()` is appropriate if you're calling `.ToList()` first since you won't have any valid groupings of less than one anyway at this point. – UtopiaLtd Nov 03 '16 at 01:26
  • Once you are going to use LINQ to Objects for grouping, it's better to use `AsEnumerable` to switch the context rather than `ToList`. – Ivan Stoev Nov 03 '16 at 09:27