Linq在新语句中获取实体的过程
我尝试在WCF服务中使用类似的内容: 我有一张有经纬度的桌子。以及来自用户的位置。在查询中,我需要从用户到报价的距离,然后按此进行订购Linq在新语句中获取实体的过程,linq,c#-4.0,linq-to-entities,wcf-ria-services,Linq,C# 4.0,Linq To Entities,Wcf Ria Services,我尝试在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 = (
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;
}
public IQueryable Get\u myOffert()
{
var User=GetCurrentPers_ID();
如果(用户!=0)
{
双lat=GetCurrentPOS().lat;
双lon=GetCurrentPOS().lon;
var query=来自this.ObjectContext.C1\u OFFERT中的c
其中c.C1\u PERS\u PERSON\u ID==用户
选择新V1\u Offert()
{
ID=c.ID,
//......
纬度=(双)c.C1\u or\u GEO.LAT,
LON=(双)c.C1_或_GEO.LON,
//这不管用
Distanz=(双)Distanze((双)c.C1\u or\u GEO.LAT,(双)c.C1\u or\u GEO.LON,LAT,LON),
半径=(双)c.距离
};
返回查询;
}
否则返回null;
}
有没有办法实现这一点?好的,我知道问题出在哪里了。LINQ to SQL正在尝试将
Distanze
转换为SQL。它不能,所以它死了。您需要在对数据库执行查询后进行投影。我尝试以下方法:
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,
Image = c.Image,
Start_Datum = c.VON,
End_Datum = c.BIS,
Name = c.C1_KEY_WORT.WORT,
Text = c.TEXT,
Preis = (decimal)c.PREIS != 0 ? (decimal)c.PREIS : 0,
WORT = c.C1_GRUP_GRUPPE.C1_KEY_WORT.WORT,
PERS_ID = (int)c.C1_PERS_PERSON_ID,
//COM_ID = (int)c.C1_COM_COMP_ID,
EH_ID = c.C1_OFF_EINHEIT_ID,
LAT = (double)c.C1_ORT_GEO.LAT,
LON = (double)c.C1_ORT_GEO.LON,
//Distanz = (double)Distanze((double)c.C1_ORT_GEO.LAT, (double)c.C1_ORT_GEO.LON, lat, lon),
Radius = (double)c.DISTANZ
//LAT = c.C1_ORT_GEO.LAT != null ? (double)c.C1_ORT_GEO.LAT : 0
};
foreach (V1_Off_Offert T in query)
{
T.Distanz = Distanze(T.LAT, T.LON, lat, lon);
}
return query;
}
else return null;
}
有没有办法使用第一种方法
也许是lambda的表达。
但将foreach的结果放入查询中并不是那么优雅 请纠正你问题中的拼写。这是对任何试图帮助你的人的侮辱。你能发布完整的例外消息吗??你只给了我们一个小片段——而且最重要的部分丢失了……你可能想考虑使用<代码> Madi.Pix>代码>,如果你写了<代码>(3.1415926538×x/180),你的代码将更可读;代码>as
转换器半径(x)代码>我不知道什么是6371
,但如果你能为它提供一些背景,你未来的自己会感谢你的(评论是一种方式,但它们只是)。好的,我现在就开始。但它也必须能够在LinQ中使用它。这是一个奇怪的要求。告诉我为什么。
foreach (V1_Off_Offert T in query)
{
T.Distanz = Distanze(T.LAT, T.LON, lat, lon);
}