Linq order by aggregate in the select { }

后端 未结 3 738
青春惊慌失措
青春惊慌失措 2021-01-04 02:27

Here is one I am working on:

var fStep =
            from insp in sq.Inspections
            where insp.TestTimeStamp > dStartTime && insp.TestTim         


        
3条回答
  •  北海茫月
    2021-01-04 03:15

    You can move the select value to a let assignment and then build an order by after that.

    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
        let newInsp = 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)
    
        }
        orderby newInsp.FailedCount, newInsp.CancelCount
        // or this ...
        //orderby newInsp.FailedCount
        //orderby newInsp.CancelCount
        select newInsp;
    ;
    

提交回复
热议问题