How to COUNT rows within EntityFramework without loading contents?

后端 未结 7 1354
一个人的身影
一个人的身影 2020-11-29 17:56

I\'m trying to determine how to count the matching rows on a table using the EntityFramework.

The problem is that each row might have many

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 18:39

    This is my code:

    IQueryable records = db.AuctionRecord;
    var count = records.Count();
    

    Make sure the variable is defined as IQueryable then when you use Count() method, EF will execute something like

    select count(*) from ...
    

    Otherwise, if the records is defined as IEnumerable, the sql generated will query the entire table and count rows returned.

提交回复
热议问题