简体   繁体   English

如何在我的Android应用程序的Visual C#Web服务中调用LINQ中的用户定义函数?

[英]How to call user defined function in LINQ in my visual C# web service for my android application?

I am currently working on an application that will retrieve other users' locations based on distance. 我目前正在开发一个应用程序,它将根据距离检索其他用户的位置。

I have a database that store all the user location information in latitude and longitude. 我有一个数据库,以纬度和经度存储所有用户位置信息。 Since the calculation of distance between these two pairs of latitude and longitude is quite complicated, I need a function to handle it. 由于这两对纬度和经度之间的距离计算非常复杂,我需要一个函数来处理它。

from a in db.Location.Where(a => (calDistance(lat, longi, Double.Parse(a.latitude), Double.Parse(a.longitude)))<Math.Abs(distance)  )) {...}

However, I got the following error: LINQ to Entities does not recognize the method and this method cannot be translated into a store expression. 但是,我收到以下错误:LINQ to Entities无法识别该方法,并且此方法无法转换为商店表达式。

I don't know how to translated it into a store expression and also, the calculation also need the math library. 我不知道如何将它翻译成商店表达式,而且,计算也需要数学库。

Is there any method that i can do to let the LINQ expression call my own function? 有没有什么方法可以让LINQ表达式调用我自己的函数?

Maybe there are other ways to achieve my goal, can anyone help? 也许有其他方法可以实现我的目标,任何人都可以帮忙吗?

LinqToEntities won't allow you to call a function, it doesn't even allow ToString() LinqToEntities不允许你调用函数,它甚至不允许ToString()

this is not a Linq thing its a LinqToEntities restriction 这不是Linq的事情,它是LinqToEntities的限制

you could put your code in to the database as a Stored Proc or Function and call it using ExecuteStoreQuery 您可以将代码作为存储过程或函数放入数据库,并使用ExecuteStoreQuery调用它

see here Does Entity Framework Code First support stored procedures? 请参阅此处实体框架代码首先支持存储过程吗?

The problem you see is that the LINQ to SQL engine is trying to inject T-SQL from your user-defined function and it cannot. 你看到的问题是LINQ to SQL引擎试图从用户定义的函数中注入T-SQL而它不能。 One (albeit nasty) option is to retrieve all of your locations and then calculate from that result set. 一个(尽管很讨厌)选项是检索所有位置,然后从该结果集中计算。

var locations = db.Location.ToList();
locations = locations.Where(a => (calDistance(lat, longi, Double.Parse(a.latitude), Double.Parse(a.longitude))).ToList();

I don't really know LINQ, but assuming that you can only send simple constraints in the query, I would construct a method that basically does the inverse of calDistance - take a coordinate and a distance and convert it into a bounding box with a minimum longitude, maximum longitude, minimum latitude, and maximum latitude. 我真的不知道LINQ,但假设你只能在查询中发送简单约束,我会构造一个基本上与calDistance相反的方法 - 获取坐标和距离并将其转换为最小的边界框经度,最大经度,最小纬度和最大纬度。

You should be able to construct a simple query that will serve your purposes with those constraints. 您应该能够使用这些约束构建一个简单的查询来满足您的目的。

something like (using Java here): 类似的东西(在这里使用Java):

public double[] getCoordinateBounds(double distance, double longitude, double latitude) {
    double[] bounds = new double[4];
    bounds[0] = longitude - distanceToLongitudePoints * (distance);
    bounds[1] = longitude + distanceToLongitudePoints * (distance);
    bounds[2] = latitude - distanceToLatitudePoints * (distance);
    bounds[3] = latitude + distanceToLatitudePoints * (distance);
    return bounds;
}

Then you could construct a query. 然后你可以构造一个查询。

double[] bounds = getCoordinateBounds(distance, longi, lat);
var nearbyUserLocations = from a in db.Location 
                         where longitude > bounds[0] and longitude < bounds[1]
                            and latitude > bounds[2] and latitude < bounds[3]

This would give you a box of points rather than a radius of points, but it would be few enough points that you could then process them and throw out the ones outside your radius. 这会给你一个点而不是一个点的半径,但它会有足够的点,然后你可以处理它们并抛出你的半径以外的点。 Or you might decide that a box is good enough for your purposes. 或者你可能会认为一个盒子足以满足你的需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用C#LINQ查询调用SQLite用户定义函数 - How to Call SQLite User Defined Function with C# LINQ Query 如何使用实体框架工作来调用Sql用户定义的函数使用LInq c#的代码优先方法 - How to call an Sql User defined Function using Entity frame work Code first approach with LInq c# 如何在我的 web 表单应用程序中调用 Azure AD 服务 - how to call Azure AD service in my web form application 从C#应用程序调用Web服务 - call Web Service from C# application 如何使用C#构建用于用户登录的RESTful Web服务API以在我的iPhone应用程序上使用 - How to build RESTful web service API for user login using C# to consume on my iPhone app 如何在Windows服务(C#)中调用我的方法? - how to call my method in windows service (c#)? C#-如何向我的项目添加SOAP Web服务引用? - C# - How to add a SOAP web service reference to my project? C#Linq SQL用户定义函数上的位置 - C# Linq Where on SQL User Defined Function 如何在我的C#应用​​程序中管理Azure API管理服务 - How to manage Azure API management service in my C# application 如何在我的C#应用​​程序中调用“连接属性”窗口并使用它? - How call Connection Properties window in my C# application and use it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM