将带有多个from的linq查询表达式转换为扩展方法语法

时间:2011-06-08 16:03:10

标签: c# linq

我无法将此代码转换为扩展方法语法:

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

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

试试这个:

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