Linq在select {}中按聚合顺序排序

时间:2010-03-31 20:28:16

标签: c# linq-to-sql

这是我正在研究的一个:

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)

            } ;

我想通过选择投影中的一个或多个字段进行排序。

3 个答案:

答案 0 :(得分:18)

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

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”,说实话 - 真正的区别在于,我们引入了一个 new 范围变量,而查询延续有效地启动了范围变量的新范围 - 你不能例如,在grp之后的位中引用into selection

值得注意的是,完全与使用两个语句相同:

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;

答案 1 :(得分:4)

将整个查询包装在括号中

.OrderBy(x => x.FailedCount).ThenBy(x => x.CancelCount);

答案 2 :(得分:1)

您可以将选择值移动到let赋值,然后在此之后构建订单。

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;
;