How to COUNT rows within EntityFramework without loading contents?

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

    Query syntax:

    var count = (from o in context.MyContainer
                 where o.ID == '1'
                 from t in o.MyTable
                 select t).Count();
    

    Method syntax:

    var count = context.MyContainer
                .Where(o => o.ID == '1')
                .SelectMany(o => o.MyTable)
                .Count()
    

    Both generate the same SQL query.

    0 讨论(0)
  • 2020-11-29 18:28

    I think you want something like

    var count = context.MyTable.Count(t => t.MyContainer.ID == '1');
    

    (edited to reflect comments)

    0 讨论(0)
  • 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();
    }
    
    0 讨论(0)
  • 2020-11-29 18:36

    I think this should work...

    var query = from m in context.MyTable
                where m.MyContainerId == '1' // or what ever the foreign key name is...
                select m;
    
    var count = query.Count();
    
    0 讨论(0)
  • 2020-11-29 18:39

    This is my code:

    IQueryable<AuctionRecord> 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.

    0 讨论(0)
  • 2020-11-29 18:40

    Well, even the SELECT COUNT(*) FROM Table will be fairly inefficient, especially on large tables, since SQL Server really can't do anything but do a full table scan (clustered index scan).

    Sometimes, it's good enough to know an approximate number of rows from the database, and in such a case, a statement like this might suffice:

    SELECT 
        SUM(used_page_count) * 8 AS SizeKB,
        SUM(row_count) AS [RowCount], 
        OBJECT_NAME(OBJECT_ID) AS TableName
    FROM 
        sys.dm_db_partition_stats
    WHERE 
        OBJECT_ID = OBJECT_ID('YourTableNameHere')
        AND (index_id = 0 OR index_id = 1)
    GROUP BY 
        OBJECT_ID
    

    This will inspect the dynamic management view and extract the number of rows and the table size from it, given a specific table. It does so by summing up the entries for the heap (index_id = 0) or the clustered index (index_id = 1).

    It's quick, it's easy to use, but it's not guaranteed to be 100% accurate or up to date. But in many cases, this is "good enough" (and put much less burden on the server).

    Maybe that would work for you, too? Of course, to use it in EF, you'd have to wrap this up in a stored proc or use a straight "Execute SQL query" call.

    Marc

    0 讨论(0)
提交回复
热议问题