LINQ为了通过聚集在选择{} [英] Linq order by aggregate in the select { }

查看:103
本文介绍了LINQ为了通过聚集在选择{}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有一个我的工作:

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;



这主要是相当于用让,说实话 - 真正的区别是,让引入了的新的的范围变量,而查询延续有效地启动一系列变量的新天地 - 你不能后参考 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为了通过聚集在选择{}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆