我有一个EmployeeSchedule
班级
public class EmployeeSchedule
{
public string EmpId { get; set; }
public string Shift { get; set; }
public DayOfWeek Day{ get; set; }
}
通过此实施:
List<EmployeeSchedule> ess = new List<EmployeeSchedule>
{
new EmployeeSchedule { EmpId = "1001", Shift = "OFF", Day = Sunday },
new EmployeeSchedule { EmpId = "1001", Shift = "MOR", Day = Monday },
new EmployeeSchedule { EmpId = "1001", Shift = "MOR", Day = Tuesday },
new EmployeeSchedule { EmpId = "1001", Shift = "MOR", Day = Wednesday },
new EmployeeSchedule { EmpId = "1001", Shift = "MOR", Day = Thursday },
new EmployeeSchedule { EmpId = "1001", Shift = "MOR", Day = Friday },
new EmployeeSchedule { EmpId = "1001", Shift = "MOR", Day = Saturday },
new EmployeeSchedule { EmpId = "1002", Shift = "SWN", Day = Sunday },
new EmployeeSchedule { EmpId = "1003", Shift = "GY", Day = Sunday },
new EmployeeSchedule { EmpId = "1004", Shift = "GY", Day = Sunday },
new EmployeeSchedule { EmpId = "1005", Shift = "MOR", Day = Sunday },
new EmployeeSchedule { EmpId = "1005", Shift = "MOR", Day = Monday },
new EmployeeSchedule { EmpId = "1005", Shift = "MOR", Day = Tuesday },
new EmployeeSchedule { EmpId = "1005", Shift = "MOR", Day = Wednesday },
new EmployeeSchedule { EmpId = "1005", Shift = "MOR", Day = Thursday },
new EmployeeSchedule { EmpId = "1006", Shift = "SWN", Day = Sunday },
new EmployeeSchedule { EmpId = "1007", Shift = "MOR", Day = Sunday }
}
我希望列表中的某个项目符合COUNT
条件:
SHIFT "MOR"
次数小于2的情况:这会让我EmpId = "1007"
SHIFT "MOR"
次数等于5的情况:这会让我EmpId = "1005"
到目前为止,我尝试了这一点,但我真的不知道在哪里或如何将COUNT
作为标准。
var id = ess.Where(x => x.Shift == "MOR").FirstOrDefault();
我怎样才能实现这个目标?
答案 0 :(得分:6)
您可以使用Where
和GroupBy
+ Count
:
var empIdGroupsWithMorShift = ess
.Where(es => es.Shift == "MOR")
.GroupBy(es => es.EmpId);
var lessTwo = empIdGroupsWithMorShift
.Where(g => g.Count() < 2)
.Select(g => new { EmpId = g.Key, Count = g.Count()} );
var five = empIdGroupsWithMorShift
.Where(g => g.Count() == 5)
.Select(g => new { EmpId = g.Key, Count = g.Count() });
如果您只想要一个字符串作为评论的结果,您可以使用String.Join
:
string five = String.Join(",", empIdGroupsWithMorShift
.Where(g => g.Count() == 5)
.Select(g => g.Key)); // "1005"
答案 1 :(得分:3)
您可以尝试使用 Linq :
var result2 = ess
.GroupBy(record => record.EmpId)
.Where(chunk => chunk.Count(record => record.Shift == "MOR") < 2)
.Select(chunk => chunk.Key);
var result5 = ess
.GroupBy(record => record.EmpId)
.Where(chunk => chunk.Count(record => record.Shift == "MOR") == 5)
.Select(chunk => chunk.Key);
// Test
// 1005
Console.Write(String.Join(Environment.NewLine, result5));
答案 2 :(得分:2)
要扩展Tim Schmelter的答案,您可以创建一种扩展方法,以便更轻松地过滤数据
public static class ExtensionMethods
{
public static string[] GetByShiftCount(this List<EmployeeSchedule> list, string shift, Func<int, bool> filter)
{
return list.Where(x => x.Shift == shift)
.GroupBy(x => x.EmpId)
.Where(g => filter(g.Count()))
.Select(g => g.Key)
.ToArray();
}
}
你可以像这样使用它
string[] EmpIds = ess.GetByShiftCount("MOR", x => x < 2);
string[] EmpIds = ess.GetByShiftCount("MOR", x => x == 5);
答案 3 :(得分:0)
获取列表,您必须致电ToList();
你可以像这样计算
var mylist = ess.Where(c=>c.Shift =="MOR").ToList();
if(mylist.count > 5
//逻辑到这里
答案 4 :(得分:0)
希望这将是解决方案(更新):
var splitResponse = xmlhttp.responseText.split( "Indiviual" );
var firstDropdownContent = splitResponse[0];
var secondDropdownContent = splitResponse[1];
// var thirdDropdownContent = splitResponse[2];
alert (firstDropdownContent);
alert (secondDropdownContent);
//alert (thirdDropdownContent);
答案 5 :(得分:0)
试试这个......
var result = ess.GroupBy(x => x.EmpId).Where(x => x.Count() < 2).SelectMany(x => x).Where(x => x.Shift == "MOR");