Entity Framework multiple counts with a single query

前端 未结 3 1884
南方客
南方客 2020-12-06 17:24

Sorry if this has been asked, but how can I improve the following with a single call to the database?

var statsModel = new
{
     Total = _db.Messages.Count(         


        
3条回答
  •  盖世英雄少女心
    2020-12-06 17:56

    In C# (rather than LINQ query), async syntax:

    var statsModel = await _db.Messages
    .GroupBy(m => 1, (g, mm) => new
    {
        Total = mm.Count(),
        Approved = mm.Count(m => m.Approved),
        Rejected = mm.Count(m => !m.Approved)
    })
    .SingleAsync();
    

提交回复
热议问题