我有这样的事情:
var groups = items.GroupBy(item => item.ListId);
foreach(var group in groups)
{
Console.WriteLine("List with ID == {0}", group.Key);
foreach(var item in group)
Console.WriteLine(" Item: {0}", item.ItemName);
}
如何在第二个foreach语句中执行where子句,例如foreach(组中的var项目,其中id == 2)用于不同的ID,例如Id == 2,其中Id == 3,依此类推。我不想使用if语句。感谢。
答案 0 :(得分:-1)
如果需要,您可以在对项目进行分组之前对其进行过滤,如下所示:
var excludeItems = new List<int>{1, 2, 3};
var groups = items
.Where(item => !excludeItems.Contains(item.ListId))
.GroupBy(item => item.ListId);
或者你可以在foreach
这样做,假设它有Id
memeber:
// 1 or some other Id or you can use the `Contains` approach
// here as well as I showed above
foreach(var item in group.Where(x => x.Id == 1))
{
//...
}