将 linq 查询作为对象发送到服务器
我有一些像这样的 linq 查询:
from x in from dbcontext
join y in dbcontext on x.id equals y.id select new {x,y}
我的问题是如何将这些类型的查询作为对象发送到服务器这里是我的 linq 查询是直接数据访问我需要将它们发送到我的数据服务器但我需要一个对象。我怎样才能实现这个?
I have some linq query like this :
from x in from dbcontext
join y in dbcontext on x.id equals y.id select new {x,y}
my problem is how I can send these kinds of query to server as an object here is my linq query is direct data-access I need to send them to my data-server but I need an object. how I can implement this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
糟糕的是。 Linq 查询包含对上下文的引用。您在客户端上没有上下文,即使有,服务器上也会有不同的实例。做这样的事情需要付出巨大的努力。此外,您还将投影返回到匿名类型 - 远程执行时另一个非常复杂的功能。
这当然可以处理 - WCF 数据服务已经做到了这一点。它们具有上下文的客户端“代理”,查询被序列化为 OData 查询字符串,并且表达式树是根据服务器端的该字符串构建的。他们也支持预测,但不支持 STE。尝试WCF 数据服务。
如果您不喜欢 WCF 数据服务的想法,您应该在服务器端为每个此类查询公开一个方法,并且客户端必须远程调用该方法。
Badly. Linq query contains references to the context. You don't have the context on the client and even if you have it will be different instance on the server. Doing anything like that would require damn big effort. Also you are returning projection to anonymous type - another very complicated feature when doing it remotely.
This can be of course handled - WCF Data Services already did this. They have client side "proxy" of context, query is serialized as OData query string and expression tree is built from that string on the server side. They also supports projections but they don't support STEs. Try WCF Data Services.
If you don't like the idea of WCF Data Services you should expose a method for each such query on the server side and client must call that method remotely.
首先,IIRC你不能序列化表达式,所以首先看看这个< /a>
然后发布类似以下内容的内容:
用法:
DR
firstly, IIRC you can't serialize Expressions, so firstly take a look at this post
then something along the lines of the following:
Usage:
D.R
由于表达式树不可序列化,因此无法轻松完成。
我的数据访问层不是用 EF 构建的,因此我的解决方案可能对您有帮助,也可能没有帮助,但无论如何:
我将客户端的表达式解析为自制的可序列化表达式(具有“列”、“连接”、“过滤器”等属性),并在服务器端根据这些表达式创建 sql。
当然,您可以从它们创建表达式,并将它们发送到 EF linq 提供程序!
Since Expression Trees are not serializable, it cannot be easily done.
My data access layer is not built with EF, so my solution may or may not be helpful to you, but here it is anyway:
I parse the expression on client side to a self-made serializable expression (which has properties like "columns", "joins", "filters", etc), and on the serverside I create sql based on these expressions.
You can create expressions from them of course, and send them against an EF linq provider!