linq查询从变量B中的列y共享变量A中的列x

时间:2012-12-13 14:23:15

标签: c# linq

我想在2个IQueryable变量中共享2列。

我有以下两个变量:

var leavingDays = resourcePlanningsInWeek.Where(rp => rp.Project.IsLeave);

var employeeRostersInWeek = employeeRostersExcludedHolidays
                .Where(rp => rp.RosterDate >= startDate &&
                             rp.RosterDate <= endDate);

我想做这样的事情:

  

leavingDays.RosterHours / employeeRostersInWeek.DeploymentPlanned   其中leavingDays.PlanDate等于employeeRostersInWeek.RosterDate

最后,我想总结所有价值观。

9/9 = 1

9/9 = 1

4,5 / 9 = 0,5

linq查询的结果(Sum all values)= 2,5

1 个答案:

答案 0 :(得分:0)

我认为应该这样做:

var result =
    (from l in leavingDays
        from r in employeeRostersInWeek
        where l.PlanDate == r.RosterDate
        select l.RosterHours / r.DeploymentPlanned
    ).Sum();

但是,如果将RosterHoursDeploymentPlanned定义为int并且您希望得到小数结果,则应首先将其中一个转换为小数数据类型,如下所示:< / p>

var result =
    (from l in leavingDays
        from r in employeeRostersInWeek
        where l.PlanDate == r.RosterDate
        select Convert.ToDouble(l.RosterHours) / r.DeploymentPlanned
    ).Sum();