gpt4 book ai didi

Linq group by + where for each group

转载 作者:行者123 更新时间:2023-12-04 07:03:30 28 4
gpt4 key购买 nike

我想编写一个 linq 表达式,该表达式将返回不包含特定值的 ID。例如,我想返回所有不具有 Value = 30 的不同 ID。

ID, Value  
1, 10
1, 20
1, 30
2, 10
2, 20
3, 10
3, 20

结果应该是 2 和 3,因为其中没有一个值为 30。

这可能与单个表达式有关吗?

谢谢

最佳答案

当然,这会做到:

var query = from i in list
group i by i.GroupId into g
where g.Any(p => p.ItemId == 30) == false
select g.Key;

foreach(var result in query) { Console.WriteLine(result); }

这输出:
2
3

这里我用过,作为一个例子:
class Product {
public int GroupId { get; set; }
public int ItemId { get; set; }
}


var list = new List<Product>() {
new Product() {GroupId = 1, ItemId = 10},
new Product() {GroupId = 1, ItemId = 20},
new Product() {GroupId = 1, ItemId = 30},
new Product() {GroupId = 2, ItemId = 10},
new Product() {GroupId = 2, ItemId = 20},
new Product() {GroupId = 3, ItemId = 10},
new Product() {GroupId = 3, ItemId = 20},
};

关于Linq group by + where for each group,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1460967/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com