我敢肯定有写下面的一个更好的办法很乐意就如何最好地去了解它如何处理空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