2

我有一个如下模型:

public class Customer
{
    public int Id { get; set; }

    public string Name { get; set; }

    public ICollection<Order> Orders { get; set; }
}

public class Order
{
    public int Id { get; set; }

    public DateTime DateTime { get; set; }

    public Customer Customer { get; set; }

    public ICollection<OrderLine> OrderLines { get; set; }
}

public class OrderLine
{
    public int Id { get; set; }

    public Product Product { get; set; }

    public int Price { get; set; }

    public int Quantity { get; set; }
}

public class Product
{
    public int Id { get; set; }

    public string Name { get; set; }

    public Category Category { get; set; }
}

public class Category
{
    public int Id { get; set; }

    public string Name { get; set; }
}

我正在使用这个基础设施

我的聚合根是客户、订单、产品。我没有在此处包含映射,因为它们是直截了当的。

var customers = unitOfWork.Customers.FindAll();
var orders = unitOfWork.Orders.FindAll();
var products = unitOfWork.Products.FindAll();

var query = ......

使用 LINQ,您将如何选择所有订购“饮料”类别产品的客户?

我在网上看到的所有示例都是非常基本的查询,没有什么高级的。

4

2 回答 2

1

i found http://msdn.microsoft.com/en-us/vbasic/bb737909

May be your query should look like:

from c in unitOfWork.Customers
join o in unitOfWork.Orders on o.Customer = c
join ol in unitOfWork.OrderLines on ol.Order = o
where ol.Product.Category.Name == "Beverages"
select c

And it is necessary to add all parent-object-properties

于 2011-02-17T11:20:38.340 回答
0

这可能有效或无效:

from customer in customers
where customer.Orders.Any(
      o => o.OrderLines.Any(l => l.Product.Category.Name == "Beverages")
select customer

(我假设您忘记了产品和类别之间的关系)

于 2011-02-17T11:28:41.747 回答