开发者

EF 4 CTP 5 Complex query

I have a model like the following:

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

I am using this infrastructure.

My aggregate roots are Customer, Orde开发者_开发知识库r, Product. I did not include the mappings here as they are straight forward.

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

var query = ......

Using LINQ, how would you select all customers that have orders for products in the "Beverages" category?

All samples I have seen on the web are very basic queries nothing advanced.


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


This might work or not:

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

(I'm assuming you forgot the relationship between Product and Category)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜