[英]Using 'out' in Linq query
我正在使用Component
對象List
componentList
。
Component
具有 GetPosition 方法,該方法通過component.GetPosition(out position, out orientation)
GetPosition
) 返回組件的位置。 我可以通過position.X, position.Y, position.Z
得到 X, Y, Z。
我有一個單獨的List<CSVPart>
從 CSV 文件導入。 每個列表項也有 X、Y、Z。我想從 CSV 部件列表中找到與 X、Y、Z 匹配的Component
。
我努力了:
foreach (CSVPart p in csvParts)
{
foundComponent = componentList
.Where(c => c.Name == p.PartNumber & ... == p.X & ... == p.Y & ... == p.Z
)
}
其中 Name 對應 PartNumber 而 ... 對應於我茫然地盯着屏幕。 我嘗試嵌套后續語句以比較 {} 中的 X、Y、Z,但我嘗試過的沒有任何效果。 如何將out
結果放入此 Linq 查詢? 提前感謝您的幫助。
我建議你不要試圖用一個表達式來做。 相反,要么編寫一個執行所需匹配的方法並在查詢中引用它,要么使用塊體 lambda:
foreach (CSVPart p in csvParts)
{
var foundComponent = componentList.FirstOrDefault(c =>
{
// Avoid finding the position if the name doesn't match.
if (c.Name != p.PartNumber)
{
return false;
}
c.GetPosition(out var position, out var _);
return position.X == p.X && position.Y == p.Y && position.Z == p.Z;
});
// foundComponent will be null or the first match
}
(顧名思義,我已從Where
更改為FirstOrDefault
,這表明您正在嘗試查找單個值...)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.