1

Following from my question previously here

I used

var distinctAllEvaluationLicenses = allEvaluationLicenses.GroupBy((License => License.dateCreated)).OrderByDescending(lics => lics.Key).First();

To group the IQueryable

allEvaluationLicenses 

by using License's property 1 which is 'dateCreated'

But now, how can I order them by using a different property such as 'nLicenceID'?

Is it possible to do something like this:

var distinctAllEvaluationLicenses = allEvaluationLicenses.GroupBy((License => License.dateCreated)).OrderByDescending(lics => (sort by nLicenseID here) ).First();
Community
  • 1
  • 1
Mikk
  • 455
  • 1
  • 13
  • 19
  • Do you want to sort all of the groups, if so, what if the items in a group don't all have the same `nLicenseID `, what do you do? Or do you mean sorting by that value within each group? – Servy Apr 29 '13 at 17:24
  • What's stopping you from doing `lics.Id` in the order-by lambda? – Mohammed Hossain Apr 29 '13 at 17:27
  • "what if the items in a group don't all have the same nLicenseID" -- Yes Im aware of this, I just want to know this -> "do you mean sorting by that value within each group?" – Mikk Apr 29 '13 at 17:27

2 Answers2

0

For LINQ-to-Objects, the objects inside each group retain the ordering in which they are discovered:

The IGrouping<TKey, TElement> objects are yielded in an order based on the order of the elements in source that produced the first key of each IGrouping<TKey, TElement>. Elements in a grouping are yielded in the order they appear in source.

So: if your aim is to order the contents of each group, simply order the source:

var distinctAllEvaluationLicenses = allEvaluationLicenses
    .OrderByDescending({whatever})
    .GroupBy({etc}).First(); 

Note that this is not guaranteed to work for other LINQ sources, and note that it doesn't influence the order in which the groups are presented. To do that you could perhaps do something like:

var distinctAllEvaluationLicenses = allEvaluationLicenses
    .GroupBy({etc}).
    .OrderBy(grp => grp.Min(item => x.SomeProp)).First(); 

which would present the groups in order of the minimum SomeProp in each. Obviously adjust to max / etc as necessary.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • He specifically said this is an `IQueryable`, so it's *not* linq to objects. It's linq to sql (based on the previous question) – Servy Apr 29 '13 at 17:29
  • 1
    @Servy `IQueryable` doesn't tell us anything about the source. LINQ-to-Objects can be exposed as `IQueryable`. Oddly enough, via `.AsQueryable`. Now, the "previous question" bit - that's fine, but I'm answering *this* question, not the previous one. – Marc Gravell Apr 29 '13 at 17:30
  • As I said, as per the previous questions by this user on this issue, it's linq to sql, not linq to objects. Even if you didn't know that, the answer should (ideally) apply to any query provider if none is specified, not just one particular (and a rather unlikely) query provider. – Servy Apr 29 '13 at 17:31
0

To sort the items within the group you can use Select:

var distinctAllEvaluationLicenses = allEvaluationLicenses.GroupBy(License => License.dateCreated)
    .Select(group => group.OrderByDescending(item => item.nLicenceID));
Servy
  • 202,030
  • 26
  • 332
  • 449