我有这段代码:
public string Label { get; set; }
public bool IsSpoon(out Spoon sp)
{
sp = null;
foreach (Tool t in Tools.GetAllItems())
if ((sp = t.AllSpoons.FirstOrDefault(x => x.Label == this.Label)) != null)
break;
return sp != null;
}
如何通过 LINQ 对其进行优化?
我想到了这样的事情,但这是不允许的:
public string Label { get; set; }
public bool IsSpoon(out Spoon sp)
{
return Tools.GetAllItems().FirstOrDefault(x => (sp = x.AllSpoons.FirstOrDefault(y => y.Label == this.Label)) != null) != null;
}
最佳答案
编辑: 我没有注意到 sp
参数,这里有一个更新:
sp = Tools
.GetAllItems()
.SelectMany(x => x.AllSpoons)
.FirstOrDefault(y => y.Label == this.Label);
return sp != null;
关于c# - LINQ 根据内部集合值选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12300751/