C# 获取实体框架中的总行数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18490679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get total row count in Entity Framework
提问by Stealth Rabbi
I'm using Entity Framework to get the total row count for a table. I simply want the row count, no where clause or anything like that. The following query works, but is slow. It took about 7 seconds to return the count of 4475.
我正在使用实体框架来获取表的总行数。我只想要行数,没有 where 子句或类似的东西。以下查询有效,但速度很慢。返回 4475 的计数大约用了 7 秒。
My guess here is that it's iterating through the entire table, just like how IEnumerable.Count()extension method works.
我的猜测是它遍历整个表,就像IEnumerable.Count()扩展方法的工作方式一样。
Is there a way I can get the total row count "quickly"? is there a better way?
有没有办法可以“快速”获得总行数?有没有更好的办法?
public int GetLogCount()
{
using (var context = new my_db_entities(connection_string))
{
return context.Logs.Count();
}
}
采纳答案by Yaakov Ellis
That is the way to get your row count using Entity Framework. You will probably see faster performance on the second+ queries as there is an initialization cost the first time that you run it. (And it should be generating a Select Count()
query here, not iterating through each row).
这是使用实体框架获取行数的方法。您可能会在第二个+ 查询上看到更快的性能,因为第一次运行它时会产生初始化成本。(它应该在Select Count()
这里生成一个查询,而不是遍历每一行)。
If you are interested in a faster way to get the raw row count in a table, then you might want to try using a mini ORM like Dapperor OrmLite.
如果您对获取表中原始行数的更快方法感兴趣,那么您可能想尝试使用像Dapper或OrmLite这样的迷你 ORM 。
You should also make sure that your table is properly defined (at the very least, that it has a Primary Key), as failure to do this can also affect the time to count rows in the table.
您还应该确保您的表被正确定义(至少,它有一个主键),因为不这样做也会影响计算表中行的时间。
回答by Khan
If you have access to do so, it would be much quicker to query the sys tables to pull this information.
如果您有权这样做,查询 sys 表以提取此信息会快得多。
E.g.
例如
public Int64 GetLogCount()
{
var tableNameParam = new SqlParameter("TableName", "Logs");
var schemaNameParam = new SqlParameter("SchemaName", "dbo");
using (var context = new my_db_entities(connection_string))
{
var query = @"
SELECT ISNULL([RowCount],0)
FROM (
SELECT SchemaName,
TableName,
Sum(I.rowcnt) [RowCount]
FROM sysindexes I
JOIN sysobjects O (nolock) ON I.id = o.id AND o.type = 'U'
JOIN (
SELECT so.object_id,
ss.name as SchemaName,
so.name as TableName
FROM sys.objects SO (nolock)
JOIN sys.schemas SS (nolock) ON ss.schema_id = so.schema_id
) SN
ON SN.object_id = o.id
WHERE I.indid IN ( 0, 1 )
AND TableName = @TableName AND SchemaName = @SchemaName
GROUP BY
SchemaName, TableName
) A
";
return context.ExecuteStoreQuery<Int64>(query, tableNameParam, schemaNameParam).First();
}
}
回答by Bhushan Firake
You can even fire Raw SQL query using entity framework as below:
您甚至可以使用实体框架触发原始 SQL 查询,如下所示:
var sql = "SELECT COUNT(*) FROM dbo.Logs";
var total = context.Database.SqlQuery<int>(sql).Single();