我有两个数据库表映射到我的 MVC 应用程序中的模型:
public class BuildingLocation
{
public int id { get; set; }
public double Lat { get; set; }
public double Lng { get; set; }
}
public class Building
{
public int id { get; set; }
public string title { get; set; }
//OTHER STUFF
}
使用 Linq / Entities,我试图获取建筑物列表,按与地图上给定点的距离排序。
DefaultConnection db = new DefaultConnection();
public IEnumerable<dynamic> GetBuildings(double north, double south, double east, double west)
{
double centreX = (east - west) / 2;
double centreY = (north - south) / 2;
var query = from b in db.Building
join l in db.BuildingLocation on
b.id equals l.id
select new {b.id, b.title, l.Lat, l.Lng,
dist = Math.Sqrt(((centreX - l.Lat) * (centreX - l.Lat)) + ((centreY - l.Lng) * (centreY - l.Lng)))
};
query = query.Where(l => l.Lat > west);
query = query.Where(l => l.Lat < east);
query = query.Where(l => l.Lng > south);
query = query.Where(l => l.Lng < north);
query = query.OrderBy(c => c.dist);
return query.AsEnumerable();
}
所以,显然这根本行不通。我以前从未使用过Linq。如何根据计算设置 OrderBy?