【发布时间】:2023-03-17 23:45:01
【问题描述】:
我想使用 linq 按照逻辑聚合一个可为空的 bool:
- 如果一切都是真的,那么就是真的;
- 如果全部为假则为假;
- 否则为空
这是我的代码,我无法获得 bool 聚合。
class T1
{
public string property1{get;set;}
public string property2{get;set;}
public bool? BoolProperty{get;set;}
}
///initialize a list<T1> t2 with values......
List<T1> t2 = new List<T1>();
t2.Add(new T1()
{
property1="hello",
property2="world",
BoolProperty=true
});
t2.Add(new T1()
{
property1="hello",
property2="world",
BoolProperty=false
});
List<T1> t1 = t2.GroupBy(g => new
{
g.property1,
g.property2
})
.Select(g => new T1
{
property1 = g.Key.property1,
property2 = g.Key.property2,
BoolProperty = ////can someone help? if all object in t2 are true, true; if all object in t2 are false, false; else null
///in this case i am expecting a null
}).ToList();
所以 t1 将是 "hello", "world", null; 谢谢
【问题讨论】:
-
我希望我可以选择多个答案。谢谢大家的帮助!