2

我有类似下面的模式:EF 4 CTP 5复杂的查询

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; } 
} 

我使用this infrastructure

我的聚合根源是Customer,Order,Product。因为它们很简单,所以我没有在这里包含映射。

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

var query = ...... 

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

我在网上看到的所有样本都是非常基本的查询,没有任何先进的。

+0

您的产品没有类别信息。 – 2011-02-17 11:21:42

+0

产品到类别的参考在哪里? – Nagg 2011-02-17 11:22:49

回答

1

我发现http://msdn.microsoft.com/en-us/vbasic/bb737909

可能是你的查询应该是这样的:

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 

而且有必要添加所有的父对象的属性

0

这可能会实现与否:

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

(我假设您忘记了产品a nd类别)