【发布时间】:2014-08-04 23:35:28
【问题描述】:
我目前正在使用以下方法来获取客户页面以及总数。唯一的问题是我要进行 2 次数据库访问 - 一次用于获取总计数,另一次用于获取页面的实际行数。
我的问题是:我可以将 totalcount 查询与实际行查询结合起来,以便 Entity Framework 在一次数据库行程中发送这两个查询吗?
public IList GetPageOfCustomers(string name, int skipCount,
int pageSize, out int totalCount) {
using(CustomerEntities e = new CustomerEntities()) {
//FIRST QUERY
var query = (from c in e.Customers
where c.NAME.Contains(name)
select new {
c.CustomerID, c.NAME, c.CITY, c.STATE, c.COUNTRY
})
.Distinct()
.OrderBy(s = > s.NAME)
.ThenBy(s = > s.CITY)
.ThenBy(s = > s.CustomerID);
//SECOND QUERY ( executed in a separate database trip)
int totalCount = (from c in e.Customers
where c.NAME.Contains(name)
select new {
c.CustomerID, c.NAME, c.CITY, c.STATE, c.COUNTRY
})
.Distinct()
.Count();
return query.Skip(skipCount).Take(pageSize).ToList();
}//END of USING
}//END of METHOD
【问题讨论】:
-
我想获取为页面行获取的相同结果集的不同计数,否则我可能会得到不准确的总计数。
-
where m.NAME.Contains(name)m是什么?不应该是c吗? -
@ATM,感谢您指出这一点。我已将 m 更改为 c。
-
@ATM,你的意思是创建一个存储过程来执行这两个查询?
-
使用Entity Framework Extended,您可以在一个包中发送多个查询。 (参见“未来查询”)。
标签: c# entity-framework-4