我有以下代码计算按车辆类型分组的车辆。
using (var uow = new UnitOfWork(new NHibernateHelper().SessionFactory)) {
var repo = new Repository<Vehicle>(uow.Session);
var vtSummary= repo.ListAll()
.GroupBy(v => v.VehicleType.Name)
.Select(v => new NameCount {
EntityDescription = v.First().VehicleType.Name,
QtyCount = v.Count() })
.OrderByDescending(v => v.QtyCount).ToList();
uow.Commit();
return vtSummary;
}
以上产生以下sql代码:
SELECT VehicleType.Name as col_0_0_,
CAST(COUNT(*) AS INT) as col_1_0_
FROM Vehicle vehicle0_
LEFT OUTER JOIN VehicleType vehicletype1_
ON vehicle0_.VehicleTypeId= VehicleType.Id
GROUP BY VehicleType.Name
ORDER BY CAST(COUNT(*) AS INT) DESC
SQL 代码在 MS SQL Server 下运行良好,但在 SQl CE 下测试会产生以下错误:
System.Data.SqlServerCe.SqlCeException : Expressions in the ORDER BY list cannot contain aggregate functions.
Sql server CE 中 Sub 查询的解决方案是为列指定别名并在 order by 子句中使用别名。
有什么方法可以在我用来使其运行而不会引发错误的 LINQ 表达式中提供别名?