开发者

Convert linq query expression with multiple froms into extension method syntax [duplicate]

This question already has answers here: Nested "from" LINQ query expressed开发者_Python百科 with extension methods (3 answers) Closed 6 years ago.

I am having trouble converting this code into 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?


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


Try this:

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜