逐字翻译是
var z = from d in db.GPSdevice
where d.CompanyId == currentuser.CompanyId && d.Type == "Trailer"
orderby d.ListOrder descending
group d by d.GeofenceLocation into g
orderby g.Count() descending
select new { Geofence = g.Key, g = (from g2 in g select g2), Count = g.Count() };
但是这不会导致完全相同的类型,原VB查询。
这里是一个更(过早?)优化版本,它导致同类型:
var z2 = (from d in db.GPSdevice
where d.CompanyId == currentuser.CompanyId && d.Type == "Trailer"
group d by d.GeofenceLocation into g
select new { Geofence = g.Key, g = (from g2 in g orderby g2.ListOrder descending select g2).ToArray(), Count = g.Count() }).OrderByDescending(g => g.Count);
小鸡蛋里挑骨头,'和'应该是'AndAlso'。 '和'是按位和操作符,而不是逻辑和。 –
@JeffMercado与一些LINQ提供者,你必须使用'And'或单独的'Where'查询,因为SQL没有任何短路的概念 –
@JacobKrall,这是不相干的,这是操作符的错误用法这个背景并没有实际的效果。如果它是相关的,则查询提供者的工作是根据数据源的要求构建查询。 –