代码之家  ›  专栏  ›  技术社区  ›  Joe Pitz

select{}中的Linq order by集合

  •  16
  • Joe Pitz  · 技术社区  · 16 年前

    下面是我正在研究的一个:

    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 回复  |  直到 16 年前
        1
  •  18
  •   Jon Skeet    16 年前

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

    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引入了 范围变量,而查询继续有效地启动了范围变量的新范围-您不能引用 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;
    
        2
  •  4
  •   David Fox    16 年前

    .OrderBy(x => x.FailedCount).ThenBy(x => x.CancelCount);
    
        3
  •  1
  •   Matthew Whited    16 年前

    您可以将select值移动到let赋值,然后在此之后构建order by。

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