How to COUNT rows within EntityFramework without loading contents?

后端 未结 7 1324
一个人的身影
一个人的身影 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条回答
  • 2020-11-29 18:48

    Use the ExecuteStoreQuery method of the entity context. This avoids downloading the entire result set and deserializing into objects to do a simple row count.

       int count;
    
        using (var db = new MyDatabase()){
          string sql = "SELECT COUNT(*) FROM MyTable where FkId = {0}";
    
          object[] myParams = {1};
          var cntQuery = db.ExecuteStoreQuery<int>(sql, myParams);
    
          count = cntQuery.First<int>();
        }
    
    0 讨论(0)
提交回复
热议问题