我有类似下面代码的逻辑。看起来,为了检查空引用,我必须有两个单独的Select语句。Linq高手能告诉我如何改进这样的查询吗?
List<C> cList = Globals.GetReferences(irrelevantThing) // GetReferences returns List<A>, which are references to B
.Select(a => a.GetB()) // GetB returns type B, can be null
.Where(b => b != null && someOtherConditions)
.Select(c => new C(b)) // C Constructor cannot have a null parameter
.ToList();
谢谢。
编辑:稍微整理了示例代码。
List<T>
,它将是面向对象的LINQ - 因此,随着额外的Select,在每次迭代中将涉及一个额外的层。 - Jon Skeet