Entity Framework multiple counts with a single query

前端 未结 3 1842
南方客
南方客 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:57

    This might help:

    var statsModel =(
            from message in _db.Messages
            group message by 1 into g
            select new
            {
                Total = g.Count(),
                Approved =g.Count (x =>x.Approved),
                Rejected =g.Count (x =>!x.Approved)
            }
        ).FirstOrDefault();
    

提交回复
热议问题