Convert a linq query expression with multiple arguments to extension method syntax

I am having trouble converting this code to the extension method syntax:

var query = from c in _context.Customers
                        from o in c.Orders
                        where o.DateSent == null
                        select new CustomerSummary
                        {
                            Id = c.Id,
                            Username = c.Username,
                            OutstandingOrderCount = c.Orders.Count
                        };

Any ideas?

+3
source share
2 answers
var query = _context.Customer
  .Where(c => c.Orders.Any(o => o.DateSent == null))
  .Select(c => new CustomerSummary
  {
    Id = c.Id,
    Username = c.Username,
    OutstandingOrderCount = c.Orders.Count(o => o.DateSent == null)
  };
+2
source

Try the following:

        var query =
            _context.Customers.SelectMany(c => c.Orders, (c, o) => new {c, o}).Where(@t => o.DateSent == null)
                .Select(@t => new CustomerSummary
                                 {
                                     Id = c.Id,
                                     Username = c.Username,
                                     OutstandingOrderCount = c.Orders.Count
                                 });
+1
source

All Articles