可以说我有一个数组说明代码:使用FirstorDefault()
Product[] myProducts = new Product[]
{
new Product { ID = 1, name = "Ketchup1", category = "Sauces", price = 200.00m },
new Product { ID = 2, name = "Ketchup2", category = "Sauces", price = 200.00m },
new Product { ID = 3, name = "Ketchup3", category = "Sauces", price = 200.00m }
};
一些项目然后可以说,我尝试使用这种方法
public Product GetProductById(int id)
{
var product = products.FirstOrDefault((p) => p.Id == id);
if (product == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return product;
}
我必须找回从阵列中检索项阅读它做什么,但我没有得到这里发生了什么:
FirstorDefault(p => p.Id == id);
参见:http://msdn.microsoft.com/en-us/library/vstudio/bb311046.aspx – 2013-04-08 07:41:37
它创建一个临时变量类型的产品,然后比较其'Id'属性与给定值 – Sandy 2013-04-08 07:41:41
这是一个lambda表达式,[检查它](http://msdn.microsoft.com/en-gb/library/bb397687.aspx) – fbstj 2013-04-08 07:41:48