Linq to实体在新声明中获得程序

时间:2012-03-19 12:56:06

标签: 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 = (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;
}

有没有办法实现这个目标?

2 个答案:

答案 0 :(得分:0)

好的,我知道问题是什么。 LINQ to SQL正在尝试将Distanze转换为SQL。它不能,所以它死了。在已经对数据库执行查询后,您需要进行投影。

答案 1 :(得分:0)

我试试这个:

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;
    }

这个作品:

foreach (V1_Off_Offert T in query)
            {
                T.Distanz = Distanze(T.LAT, T.LON, lat, lon);                    
            }

有没有办法使用第一种方式 也许是lambda表达。 但是将foreach的结果放入查询中并不是那么优雅!!!