Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq order by aggregate in the select { }

Tags:

c#

linq-to-sql

Here is one I am working on:

var fStep =
            from insp in sq.Inspections
            where insp.TestTimeStamp > dStartTime && insp.TestTimeStamp < dEndTime
                && insp.Model == "EP" && insp.TestResults != "P"
            group insp by new { insp.TestResults, insp.FailStep } into grp

            select new
            {
                FailedCount = (grp.Key.TestResults == "F" ? grp.Count() : 0),
                CancelCount = (grp.Key.TestResults == "C" ? grp.Count() : 0),
                grp.Key.TestResults,
                grp.Key.FailStep,
                PercentFailed = Convert.ToDecimal(1.0 * grp.Count() /tcount*100)

            } ;

I would like to orderby one or more of the fields in the select projection.

like image 591
Joe Pitz Avatar asked Mar 31 '10 20:03

Joe Pitz


People also ask

How to order by ascending in LINQ?

In LINQ, the OrderBy operator is used to sort the list/ collection values in ascending order. In LINQ, if we use order by the operator by default, it will sort the list of values in ascending order. We don't need to add any ascending condition in the query statement.

What is OrderByDescending?

Sorts the elements of a sequence in descending order according to a key. OrderByDescending<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IComparer<TKey>) Sorts the elements of a sequence in descending order by using a specified comparer.


2 Answers

The simplest change is probably to use a query continuation:

var fStep =
        from insp in sq.Inspections
        where insp.TestTimeStamp > dStartTime && insp.TestTimeStamp < dEndTime
            && insp.Model == "EP" && insp.TestResults != "P"
        group insp by new { insp.TestResults, insp.FailStep } into grp
        select new
        {
            FailedCount = (grp.Key.TestResults == "F" ? grp.Count() : 0),
            CancelCount = (grp.Key.TestResults == "C" ? grp.Count() : 0),
            grp.Key.TestResults,
            grp.Key.FailStep,
            PercentFailed = Convert.ToDecimal(1.0 * grp.Count() /tcount*100)

        } into selection
        orderby selection.FailedCount, selection.CancelCount
        select selection;

That's mostly equivalent to using "let", to be honest - the real difference is that let introduces a new range variable, whereas a query continuation effectively starts a new scope of range variables - you couldn't refer to grp within the bit after into selection for example.

It's worth noting that this is exactly the same as using two statements:

var unordered =
        from insp in sq.Inspections
        where insp.TestTimeStamp > dStartTime && insp.TestTimeStamp < dEndTime
            && insp.Model == "EP" && insp.TestResults != "P"
        group insp by new { insp.TestResults, insp.FailStep } into grp
        select new
        {
            FailedCount = (grp.Key.TestResults == "F" ? grp.Count() : 0),
            CancelCount = (grp.Key.TestResults == "C" ? grp.Count() : 0),
            grp.Key.TestResults,
            grp.Key.FailStep,
            PercentFailed = Convert.ToDecimal(1.0 * grp.Count() /tcount*100)

        };

var fStep = from selection in unordered
            orderby selection.FailedCount, selection.CancelCount
            select selection;
like image 150
Jon Skeet Avatar answered Oct 24 '22 13:10

Jon Skeet


wrap the whole query in parentheses and

.OrderBy(x => x.FailedCount).ThenBy(x => x.CancelCount);
like image 31
David Fox Avatar answered Oct 24 '22 15:10

David Fox