我有下面的语句,它总是返回null:为什么Null无效的LINQ投影?
var addins = allocations.SelectMany(
set => set.locations.Any(q => q.IsMatch(level, count))
? (List<string>)set.addins : null
);
我改变了它略显不足,现在它工作正常:
var addins = allocations.SelectMany(
set => set.locations.Any(q => q.IsMatch(level, count))
? set.addins : new List<string>()
);
我的主要问题:不为空,为什么可以作为在LINQ的这个上下文中来自三元运算符的返回类型?
第二个问题:是否有一种更聪明的方式来制定上述查询(特别是如果它消除了“新List()”)?
优秀的答案和见解。顺便说一句,“set.addins”在你的其他例子中不是必须的,因为三元运算符没有涉及。 – 2010-09-22 03:27:23