【发布时间】:2012-04-03 23:07:22
【问题描述】:
我尝试在 WCF 服务中使用类似的东西:
我有一张带有纬度和经度报价的表格。以及来自用户的位置。在查询中,我需要从用户到报价的距离,并为此订购。
private double Distanze(double LAT1, double LON1, double LAT2, double LON2)
{
double e = (3.1415926538 * LAT1 / 180);
double f = (3.1415926538 * LON1 / 180);
double g = (3.1415926538 * LAT2 / 180);
double h = (3.1415926538 * LON2 / 180);
double i = (Math.Cos(e) * Math.Cos(g) *
Math.Cos(f) * Math.Cos(h) + Math.Cos(e) *
Math.Sin(f) * Math.Cos(g) * Math.Sin(h) +
Math.Sin(e) * Math.Sin(g));
double j = (Math.Acos(i));
double k = (6371 * j);
return k;
}
在查询中:
public IQueryable<V1_Off_Offert> Get_myOffert()
{
var User = GetCurrentPers_ID();
if (User != 0)
{
double lat = GetCurrentPOS().LAT;
double lon = GetCurrentPOS().LON;
var query = from c in this.ObjectContext.C1_OFF_OFFERT
where c.C1_PERS_PERSON_ID == User
select new V1_Off_Offert()
{
ID = c.ID,
//......
LAT = (double)c.C1_ORT_GEO.LAT,
LON = (double)c.C1_ORT_GEO.LON,
//This it dosnt Work
Distanz = (double)Distanze((double)c.C1_ORT_GEO.LAT, (double)c.C1_ORT_GEO.LON, lat, lon),
Radius = (double)c.DISTANZ
};
return query;
}
else return null;
}
有没有办法实现这一点?
【问题讨论】:
-
请修正您问题中的拼写。这是对任何试图帮助你的人的侮辱。
-
您能否发布完整的异常消息?你只给了我们一个小小的 sn-p - 最重要的部分不见了......
-
你可能想考虑使用
Math.Pi,如果你把(3.1415926538 * x / 180);写成ConvertToRadians(x);,你的代码会更易读。我不知道6371是什么,但你未来的自己会感谢您提供一些上下文(cmets 是一种方式,但这些只是apologies)。
标签: linq c#-4.0 linq-to-entities wcf-ria-services