C# 使用实体框架获取大表的行数
我使用实体框架来获得一个包含数百万条记录的表的简单行数,我不需要任何where子句。C# 使用实体框架获取大表的行数,c#,sql-server,entity-framework,C#,Sql Server,Entity Framework,我使用实体框架来获得一个包含数百万条记录的表的简单行数,我不需要任何where子句。 我尝试使用Count方法,但获取计数需要很长时间。等待那么久,有没有什么有效的方法来让伯爵回来 您可以按如下所示进行尝试。此查询将返回IQueryable结果。这意味着在数据库中进行计数操作 基于查询的: 基于方法的: 您可以按如下所示进行尝试。此查询将返回IQueryable结果。这意味着在数据库中发生计数操作 基于查询的: 基于方法的: 我认为您首先要检索所有记录,然后对它们进行计数。你应该直接清点你的记录
我尝试使用Count方法,但获取计数需要很长时间。等待那么久,有没有什么有效的方法来让伯爵回来 您可以按如下所示进行尝试。此查询将返回IQueryable结果。这意味着在数据库中进行计数操作 基于查询的: 基于方法的:
您可以按如下所示进行尝试。此查询将返回IQueryable结果。这意味着在数据库中发生计数操作 基于查询的: 基于方法的:
我认为您首先要检索所有记录,然后对它们进行计数。你应该直接清点你的记录
using(DatabaseContext db = new DatabaseContext())
{
//what I think you are doing:
int countLong = db.UserAccount.ToList().Count();
//what you should be doing
int countShort = db.UserAccount.Count();
//include where clauses inside count, wrong way:
int countLong2 = db.UserAccount.ToList().Count(x => x.Active);
//include where clauses inside count, good way:
int countShort2 = db.UserAccount.Count(x => x.Active);
//or if you don't like lambda expressions.
int countLong3 = (from x in db.UserAccount
//where x.Active //optional
select x).ToList().Count();
int countShort3 = (from x in db.UserAccount
//where x.Active //optional
select x).Count();
}
DatabaseContext将是扩展DbContext类的类,我认为您首先要检索所有记录,然后对它们进行计数。你应该直接清点你的记录
using(DatabaseContext db = new DatabaseContext())
{
//what I think you are doing:
int countLong = db.UserAccount.ToList().Count();
//what you should be doing
int countShort = db.UserAccount.Count();
//include where clauses inside count, wrong way:
int countLong2 = db.UserAccount.ToList().Count(x => x.Active);
//include where clauses inside count, good way:
int countShort2 = db.UserAccount.Count(x => x.Active);
//or if you don't like lambda expressions.
int countLong3 = (from x in db.UserAccount
//where x.Active //optional
select x).ToList().Count();
int countShort3 = (from x in db.UserAccount
//where x.Active //optional
select x).Count();
}
DatabaseContext将是扩展DbContext类的类您有示例代码吗?如果您使用的是Count的IEnumerable版本,那么应用程序将检索所有行,然后对它们进行计数。如果您使用的是Count的IQueryable版本,则数据库服务器将进行计数,并且应该是快速的。到目前为止,您是否可以显示代码?只需对数据库执行原始SQL查询即可。这可能是实现这一目标的最快方法。你有样本代码吗?如果您使用的是Count的IEnumerable版本,那么应用程序将检索所有行,然后对它们进行计数。如果您使用的是Count的IQueryable版本,则数据库服务器将进行计数,并且应该是快速的。到目前为止,您是否可以显示代码?只需对数据库执行原始SQL查询即可。这可能是实现这一目标的最快方法。或者简单地说就是context.yourTable.count我发现这会转化为一些性能非常差的SQL,它会遍历数据库中的每一行:选择[GroupBy1]。[A1]作为[C1]从[dbo]。[Log]作为[Extent1]作为[GroupBy1]——我建议使用原始SQL,我没有找到另一种方法。或者仅仅是context.yourTable.count我发现这会转化为一些性能非常差的SQL,它会在DB中的每一行进行迭代:选择[GroupBy1]。[A1]作为[C1]从选择COUNT1作为[A1]从[dbo]。[Log]作为[Extent1]作为[GroupBy1]——我建议使用原始SQL,我还没找到别的办法。
using(DatabaseContext db = new DatabaseContext())
{
//what I think you are doing:
int countLong = db.UserAccount.ToList().Count();
//what you should be doing
int countShort = db.UserAccount.Count();
//include where clauses inside count, wrong way:
int countLong2 = db.UserAccount.ToList().Count(x => x.Active);
//include where clauses inside count, good way:
int countShort2 = db.UserAccount.Count(x => x.Active);
//or if you don't like lambda expressions.
int countLong3 = (from x in db.UserAccount
//where x.Active //optional
select x).ToList().Count();
int countShort3 = (from x in db.UserAccount
//where x.Active //optional
select x).Count();
}