Entity Framework Core count does not have optimal performance

前端 未结 9 1560
故里飘歌
故里飘歌 2020-12-20 12:19

I need to get the amount of records with a certain filter.

Theoretically this instruction:

_dbContext.People.Count (w => w.Type == 1);
         


        
9条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-20 12:43

    Try this

    (from x in _dbContext.People where x.Type == 1 select x).Count();
    

    or you could do the async version of it like:

    await (from x in _dbContext.People where x.Type == 1 select x).CountAsync();
    

    and if those don't work out for you, then you could at least make the query more efficient by doing:

    (from x in _dbContext.People where x.Type == 1 select x.Id).Count();
    

    or

    await (from x in _dbContext.People where x.Type == 1 select x.Id).CountAsync();
    

提交回复
热议问题