我想使用 linq按照逻辑聚合可空的bool: 如果一切都是真的那么真实; 如果一切都是假的那么假; 否则为null 这是我的代码,我无法得到bool聚合. class T1{ public string property1{get;set;} public stri
>如果一切都是真的那么真实;
>如果一切都是假的那么假;
>否则为null
这是我的代码,我无法得到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;
谢谢
List<T1> t1 = t2.GroupBy(g => new { g.property1, g.property2 }) .Select(g => new T1 { property1 = g.Key.property1, property2 = g.Key.property2, BoolProperty = g.GroupBy(grp => grp.BoolProperty).Count() > 1 ? null : g.Select(g_val=>g_val.BoolProperty).First() }).ToList();