最簡單的變化可能是使用查詢延續:
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;
這主要是相當於用「讓」,說實話 - 真正的區別是,讓引入了新範圍變量,而查詢延續有效地啓動了一個新的範圍變量範圍 - 例如,在into selection
之後的位中不能引用grp
。
值得一提的是,這是正是與使用兩個語句:
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;
工程蠻好 – 2010-03-31 20:52:24
這是非常好的。好,喬恩 – 2012-08-13 17:19:00