0

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

我有以下两个变量:

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

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

我想做这样的事情:

leaveDays.RosterHours / employeeRostersInWeek.DeploymentPlanned where leaveDays.PlanDate 等于employeeRostersInWeek.RosterDate

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

9/9 = 1

9/9 = 1

4,5/9 = 0,5

linq 查询的结果(所有值之和)= 2,5

4

1 回答 1

0

我认为应该这样做:

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

但是,如果RosterHoursDeploymentPlanned被定义为ints 并且您期望小数结果,那么您应该首先将其中一个转换为小数数据类型,如下所示:

var result =
    (from l in leavingDays
        from r in employeeRostersInWeek
        where l.PlanDate == r.RosterDate
        select Convert.ToDouble(l.RosterHours) / r.DeploymentPlanned
    ).Sum();
于 2012-12-13T14:35:35.497 回答