鉴于以下查询
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 =(employeeIdList中的int employeeId)
例如,如果employeeIdList和CacheObjects.AllEmployeeOtherLeaves集合中有EmployeeIds 1,2,3,则所有3名员工的日期为1/1/2001,然后检索该日期。
答案 0 :(得分: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
个对象的列表),并将其内容投影到EmployeeOtherLeaveItem
。< / p>
答案 1 :(得分:1)
在where语句中添加“&amp;&amp; employeeIdList.Contains(x.EmployeeId)”
答案 2 :(得分: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();