-2

次のようなオブジェクト階層を照会しようとしています:

顧客 -->IList注文 & 注文 -->IList製品の

Customerオブジェクトには注文のコレクションがあり、オブジェクトOrderには製品のコレクションがあります。

私がやろうとしているのは、特定の製品を注文した顧客を獲得することです。で問い合わせますproduct id。クエリの最後に、Customer list.

これを試しましたが、うまくいきませんでした。

public ICollection<Customer> GetByParticularProduct(int productId)
{

    return allCustomers
    .Where(customer => customer.Orders
.Any(order => order.Products
.Any(prod => prod.Id == productId)))
    .ToList();
}

どうすればこれを乗り越えることができますか?

4

2 に答える 2

0

コレクションに が含まれている可能性がある場合はnull、これを試してください

public ICollection<Customer> GetByParticularProduct(int productId)
{
    return (from cust in allCustomers.Where(x => x != null)
            let orders = cust.Orders.Where(x => x != null)
            let prods = orders.SelectMany(x => x.Products).Where(x => x != null)
            where prods.Any(x => x.Id == productId)
            select cust)
           .ToList();
}
于 2013-01-22T21:40:20.090 に答える
-1
return allCustomers
    .Where(customer => customer.Orders
        .Any(order => order.Products
            .Any(prod => prod.Id == productId))).ToList();

また

return allCustomers
    .Where(customer => customer.Orders
        .Any(order => order.ProductId == productId))).ToList();
于 2013-01-22T20:50:14.520 に答える