Entity Framework - Performance in count

后端 未结 5 756
清歌不尽
清歌不尽 2021-01-11 10:49

I\'ve a little question about performance with Entity Framework.

Something like

using (MyContext context = new MyContext())
{
    Document DocObject         


        
5条回答
  •  鱼传尺愫
    2021-01-11 11:28

    Your first query isnt fully transalted to sql - when you call .ToList().Count(), you are basically saying "download all, materialize it to POCO and call extension method named Count()" which, of course, take some time.

    Your second query is, however, transalted to something like select count(*) from Documents where GroupId = @DocObjectGroup which is much faster to execute and you arent materializing anything, just simple scalar.

提交回复
热议问题