var q =
from c in db.Customers
join o in db.Orders on c.CustomerID
equals o.CustomerID into orders
orderby orders.Count() descending
select new
{
c.ContactName,
OrderCount = orders.Count()
};
类似这样的排序,最后变成sql语句以后在select和order里要分别计算Count的值,这样应该是有性能问题的吧,怎样改进一下才能只计算一次呢?
var q = from c in Customers
let o = c.Orders.Count
orderby o descending
select new
{
c.ContactName,
OrderCount = o
}
用let,这样就OK,可以结案了。呵呵
参考:http://www.cnblogs.com/RuiLei/archive/2008/02/26/1082600.html