我敢肯定有寫下面的一個更好的辦法很樂意就如何最好地去了解它如何處理空LINQ查詢
幾個指針基本上我不能保證項目的清單包含特定項目我後,並試圖返回一個基於上述項目的價值。
class item
{
string Brand;
string Product;
decimal Value;
}
//List is created and filled elsewhere
List<item> _items;
void DoStuff()
{
decimal desiredValue = 0;
try
{
var XXX = _items
.Where(x=>x.Brand == "brand1")
.Where(x=>x.Product == "product1")
.First();
desiredValue = XXX.Value;
}
catch()
{
//Empty Catch Bugs me
}
//Do something with desiredValue
}
}
順便說一句,正如沒有人指出的那樣,您使用了單個'=',而c#使用'=='來實現相等性。也許只是你的問題中一個錯字,但認爲ID指出來以防萬一;) – Jamiec 2012-02-08 11:05:40
耶只是一個錯字我會解決的感謝 – RoughPlace 2012-02-08 11:10:26