我有下面的语句如何使如果语句内的LINQ
Select(g => new AssembledPartsDTO
{
..
..
References = g.SelectMany(entry => entry.References).OrderBy(t => t).ToList()
..
..
}
我如何的想法添加到if References.count == 0 than Add("??")
References
感谢。
我有下面的语句如何使如果语句内的LINQ
Select(g => new AssembledPartsDTO
{
..
..
References = g.SelectMany(entry => entry.References).OrderBy(t => t).ToList()
..
..
}
我如何的想法添加到if References.count == 0 than Add("??")
References
感谢。
使用ternary operator在你的LINQ表达。
你可以做这样的事情;
References = (g.SelectMany(entry => entry.References).Count() == 0)
? g.SelectMany(entry => entry.References).OrderBy(t => t).ToList() : null;
另一种方式:
Select(g =>
{
var obj = new AssembledPartsDTO { References= ... };
if (obj.References.Count == 0)
{
obj.References.Add("??");
}
return obj;
})
您可能需要这一个更复杂的逻辑,其中三元运算符不适用(?)。
应该指出的是,这只适用于LINQ to Objects。 –