Linq order by 在 select { } 中聚合

时间:2023-02-04
本文介绍了Linq order by 在 select { } 中聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

这是我正在研究的一个:

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.

推荐答案

最简单的更改可能是使用查询延续:

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;

老实说,这几乎等同于使用let"——真正的区别在于 let 引入了一个范围变量,而查询继续有效地启动了一个新的范围变量范围——你可以例如,在into selection之后的位内不要引用grp.

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;

这篇关于Linq order by 在 select { } 中聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:Linq to SQL:调用 SubmitChanges() 时的执行顺序 下一篇:使用 LinqPad 将字符串转换为 Guid

相关文章

最新文章