基本上,我试图从数据库中检索对象列表(使用实体框架6),但是我希望以某种方式对项目进行排序。
我尝试了以下几点:
context.Coordinates.OrderBy(x =>
{
double latDif = Math.Abs(centerLat - x.Longtitude);
double lngDif = Math.Abs(centerLng - x.Latitude);
double dif = latDif + lngDif;
return dif;
});
但是,编译器显示以下错误:
带有语句体的lambda表达式不能转换为表达式树。
我已经查过这一点,并明白在linq-to-sql中调用orderby时不能使用语句体(花括号)。
然而,如何才能在不加载所有条目的情况下,执行像上面这样的复杂命令呢?
顺便说一句,如果您想知道,我正在尝试按最接近中心和弦的项目(centerLat和centerLng)来订购这些项目。
发布于 2016-02-19 19:57:10
你可以做到这一点。
context.Coordinates.OrderBy(x=> Math.Abs(centerLat - x.Longtitude) + Math.Abs(centerLng - x.Latitude));
如果您正在寻找基于多列的订单,那么请使用
.OrderBy(x=> new {
// fields or props or columns
})
https://stackoverflow.com/questions/35518826
复制相似问题