[RESOLVED] Help optimising LINQ query is needed
I've a class
Code:
Class DeclInfo
{
public int Year {get; set;}
public int Quarter {get; set;}
public int CorrectionNumber {get; set;}
// ...
} // class DeclInfo
I'm writing a method FindFirst which queries a collection of DeclInfo to find an earliest instance:
Find earliest year, then find earliest quarter and then find the latest (max) correction number.
Now I have the following:
Code:
int Criteria = allDecls.Min(d => d.Year);
allDecls = allDecls.Where(d => d.Year == Criteria).ToList();
Criteria = allDecls.Min(d => d.Quarter);
allDecls = allDecls.Where(d => d.Quarter == Criteria).ToList();
Criteria = allDecls.Max(d => d.CorrectionNumber);
di = allDecls.First(d => d.CorrectionNumber == Criteria);
I don't like what I see very much and therefore - a question - is there a way to make it simpler?
Re: Help optimising LINQ query is needed
Here's an alternative using OrderBy and ThenBy().
Code:
DeclInfo info = new DeclInfo();
info = declinfosDummy
.OrderBy(t => t.Year)
.ThenBy(t => t.Quarter)
.ThenByDescending(t => t.CorrectionNumber)
.ToList().First();
I tested the output produced by your LINQ statements against my LINQ statement. The results were identical.