2

鉴于以下查询

       public TrainingListViewModel(List<int> employeeIdList)
        {
            this.EmployeeOtherLeaveItemList =
                CacheObjects.AllEmployeeOtherLeaves
                .Where(x => x.OtherLeaveDate >= Utility.GetToday() &&
                    x.CancelDate.HasValue == false &&
                    x.OtherLeaveId == Constants.TrainingId)
                .OrderBy(x => x.OtherLeaveDate)
                .Select(x => new EmployeeOtherLeaveItem
                {
                    EmployeeOtherLeave = x,
                    SelectedFlag = false
                }).ToList();
        }

我想将employeeIdList 放入查询中。我想检索所有 x.OtherLeaveDate 值,其中每个连接都存在相同的 x.OtherLeaveDate 其中 x.EmployeeId = (int employeeId in employeeIdList)

例如,如果在 employeeIdList 和 CacheObjects.AllEmployeeOtherLeaves 集合中有 EmployeeIds 1、2、3,则所有 3 名员工的日期都是 2001 年 1 月 1 日,然后检索该日期。

4

3 回答 3

2

如果我读得很好,应该是这样的

var grp = this.EmployeeOtherLeaveItemList =
    CacheObjects.AllEmployeeOtherLeaves
    .Where(x => x.OtherLeaveDate >= Utility.GetToday()
             && x.CancelDate.HasValue == false
             && x.OtherLeaveId == Constants.TrainingId
             && employeeIdList.Contains(x.EmployeeId)) // courtesy @IronMan84
    .GroupBy(x => x.OtherLeaveDate);

if (grp.Count() == 1)
{
    var result = g.First().Select(x => new EmployeeOtherLeaveItem
    {
        EmployeeOtherLeave = x,
        SelectedFlag = false
    })
}

首先,数据按 分组OtherLeaveDate。如果分组结果恰好是一个组,则采用第一个(也是唯一的)IGrouping实例(它是一个Leave对象列表)并将其内容投影到EmployeeOtherLeaveItems。

于 2012-10-21T15:20:23.413 回答
1

在 where 语句中添加“&& employeeIdList.Contains(x.EmployeeId)”

于 2012-10-19T14:51:58.873 回答
0

我需要感谢@IronMan84 和@GertArnold 帮助我,我将不得不告诫自己在问题上没有更清楚。这是我想出的答案。毫无疑问,它可以改进,但没有人回答说为什么我现在要勾选这个答案。

   var numberOfEmployees = employeeIdList.Count;


var grp = CacheObjects.AllEmployeeOtherLeaves.Where(
                    x =>
                    x.OtherLeaveDate >= Utility.GetToday() 
                    && x.CancelDate.HasValue == false
                    && x.OtherLeaveId == Constants.TrainingId 
                    && employeeIdList.Contains(x.EmployeeId))
                    .GroupBy(x => x.OtherLeaveDate)
                    .Select(x => new { NumberOf = x.Count(), Item = x });

            var list =
                grp.Where(item => item.NumberOf == numberOfEmployees).Select(item => item.Item.Key).ToList();
于 2012-10-22T15:02:52.770 回答