大佬教程收集整理的这篇文章主要介绍了c# – 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();
谢谢.
编辑:稍微清理一下示例代码.
var queryB = from a in Globals.GetReferences(irrelevantThing) let b = a.GetB() where b != null && someOtherConditions SELEct new B(b); var bList = queryB.ToList()
取决于你的喜好…
FYI在扩展方法语法中,上面转换为
var queryB = Globals.GetReferences(irrelevantThing) .SELEct(a => new { a,b = a.GetB() }) .Where(x => x.b != null && someOtherConditions) .SELEct(x => new B(x.b)) .ToList();
更新:刚才意识到你也可以使用SELEct into子句从字面上翻译确切的原始查询:
var queryB = from a in Globals.GetReferences(irrelevantThing) SELEct a.GetB() into b where b != null && someOtherConditions SELEct new B(b);
仍然喜欢让…
以上是大佬教程为你收集整理的c# – Linq“选择”理智问题全部内容,希望文章能够帮你解决c# – Linq“选择”理智问题所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。