There are classes of models

public class Product { public int ProductID { get; set; } public string ProductName { get; set; } public string Category { get; set; } public decimal UnitPrice { get; set; } public int UnitsInStock { get; set; } } public class Order { public int OrderID { get; set; } public DateTime OrderDate { get; set; } public decimal Total { get; set; } } 

It is necessary to get all customers who have orders that exceed the sum value

I made the following request

  var customers = from c in dataSource.Customers from o in c.Orders where o.Total > value select c).Distinct(); 

Displays what you need.

It is necessary to execute this query in the dot notation, I try to do this:

  var customers = dataSource.Customers.Select(c => c.Orders .Where(o => o.Total > value)) .Distinct(); 

it turns out that the query returns IEnumerable<IEnumerable<Order>> , and I need IEnumerable<Customers> . Please tell me where I am wrong, and how to make requests of this kind correctly.

    2 answers 2

    You need to write this:

     var customers = dataSource.Customers.Where(c => c.Orders.Any(o => o.Total > value)).Distinct(); 

    If you need at least one order more value , or so:

     var customers = dataSource.Customers.Where(c => c.Orders.All(o => o.Total > value)).Distinct(); 

    If you want all orders more value .

    In this case, you should not use the Select method, since it returns a new collection.

      Two from row, in the query form correspond to the method SelectMany

      The request can take the form:

       var customers = dataSource.Customers .SelectMany(c => c.Orders, (c,o) => new {c, o}) .Where(item => item.o.Total > value) .Select(item => item.c) .Distinct();