问题描述
这是我正在研究的一个:
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;