Linq通过检索百分比查询组

时间:2010-01-12 11:14:04

标签: c# linq group-by

我必须计算group by子句中的百分比。 更深入的我需要计算Status ='False'的ControlForms的百分比。状态在表checkResult中。

var items = (from controlForm in dataContext.ControlForms
             join controlFormStatus in dataContext.ControlFormStatus on controlForm.FK_ControlFormStatus equals controlFormStatus.Id
             join docCheck in dataContext.DocumentChecks on controlForm.Id equals docCheck.FK_ControlForm
             join checkResult in dataContext.CheckResults on docCheck.FK_CheckResult equals checkResult.Id
             where controlForm.FK_ControlCycle.ToString().Equals(controlCycleId) & (
             controlFormStatus.Description.Equals(Constants.ControlFormStatusTestControlOwner)
             | controlFormStatus.Description.Equals(Constants.ControlFormStatusApprovalProcessOwner))
             group controlForm by new { controlForm.Id, controlForm.FK_ControlCycle, controlForm.FK_SampleNode, controlForm.FK_Control, /*FUNCTION*/} into ctrlForm
                         select new ControlFormReportPartialResults(ctrlForm.Key.Id, ctrlForm.Key.FK_ControlCycle, ctrlForm.Key.FK_SampleNode, ctrlForm.Key.FK_Control, ValueFunction));

任何人都可以建议我拥有的东西,而不是/ FUNCTION /?或者,如果有另一种方法来解决问题? 我们非常欢迎任何帮助!

提前致谢, Dario Zanelli

1 个答案:

答案 0 :(得分:1)

将“collection”替换为包含要计算百分比的项目的相应变量:

collection.Select(item => item.Status == false).Count() / collection.Count() * 100