EF 4 CTP 5复杂查询

时间:2011-02-17 11:12:55

标签: entity-framework entity-framework-4 code-first ef-code-first entity-framework-ctp5

我有一个如下模型:

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

我的根源是客户,订单,产品。我没有在这里包含映射,因为它们是直接的。

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

var query = ......

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

我在网上看到的所有样本都是非常基本的查询。

2 个答案:

答案 0 :(得分: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

有必要添加所有父对象属性

答案 1 :(得分:0)

这可能有效:

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

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