4

配列にいくつかのアイテムがあるとしましょう

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);
4

2 に答える 2