我想這個foreach循環移動到LINQ:FirstOrDefault Linq中
compData = componentData[0];
foreach (var componentTraceData in componentData)
{
if (!string.IsNullOrEmpty(componentTraceData.CompName))
{
compData = componentTraceData;
break;
}
}
這是我的嘗試:
var tt = (from n in componentData
where !string.IsNullOrEmpty(n.CompName)
select n).FirstOrDefault();
我怎樣才能使它採取componentData[0]
的情況下,LINQ不發現任何結果?
[FirstOrDefault返回null時,返回列表中的第一項可能重複](http://stackoverflow.com/questions/9429341/return-first-item-in-list-if-firstordefault-returns-null) – 2012-07-11 14:37:20