我正在處理一個List物件Component,componentList.
Component具有GetPosition通過 . 回傳組件位置的方法component.GetPosition(out position, out orientation)。我可以通過position.X, position.Y, position.Z.
我有一個單獨List<CSVPart>的從 CSV 檔案匯入的檔案。每個串列項也有 X、Y、Z。我想Component從 CSV 部件串列中找到與 X、Y、Z 匹配的項。
我努力了:
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 查詢?提前感謝您的幫助。
uj5u.com熱心網友回復:
我建議你不要試圖用一個運算式來做。相反,要么撰寫一個執行所需匹配的方法并在查詢中參考它,要么使用塊體 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/484094.html
