2

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, Order, 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.

Community
  • 1
  • 1
Cosmin Onea
  • 2,698
  • 1
  • 24
  • 27

2 Answers2

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

Sergey Vedernikov
  • 7,609
  • 2
  • 25
  • 27
0

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)

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154