【问题标题】:Linq to entities get procedure in new statementLinq to entity 在新语句中获取过程
【发布时间】: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


【解决方案1】:

我试试这个:

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的Result放入查询中并不是那么优雅!!!

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 好的,我现在。但它也必须能够在 LinQ 中使用它。
    • 这是一个奇怪的要求。告诉我为什么。
    猜你喜欢
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多