How to COUNT rows within EntityFramework without loading contents?

后端 未结 7 1343
一个人的身影
一个人的身影 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:31

    As I understand it, the selected answer still loads all of the related tests. According to this msdn blog, there is a better way.

    http://blogs.msdn.com/b/adonet/archive/2011/01/31/using-dbcontext-in-ef-feature-ctp5-part-6-loading-related-entities.aspx

    Specifically

    using (var context = new UnicornsContext())
    
        var princess = context.Princesses.Find(1);
    
        // Count how many unicorns the princess owns 
        var unicornHaul = context.Entry(princess)
                          .Collection(p => p.Unicorns)
                          .Query()
                          .Count();
    }
    

提交回复
热议问题