选择int数组中ID为ID的实体-WCF Data Services,LINQ [英] Select entities where ID in int array - WCF Data Services, LINQ

查看:65
本文介绍了选择int数组中ID为ID的实体-WCF Data Services,LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用LINQ和数据服务返回一组ID包含在ID列表或ID数组中的实体.我知道如何使用LinqToEF做到这一点,但是我不知道如何使用数据服务或使用OData查询约定来实现这一点.

I would like to return a set of entities who has and ID that is contained in a list or array of IDs using LINQ and Data Services. I know how to this using LinqToEF but I am at a loss how to this with Data Services or using OData query conventions for that matter.

我的想法是我会做类似的事情:

My thought is that I would do something like:

int[] intArray = {321456, 321355, 218994, 189232};
var query = (from data in context.Entity
             where intArray.contains(data.ID)
             select data);

使用Data Services/OData有什么方法可以完成吗?我知道我可能可以通过Service Operation破解它,但我不希望这样做.

Is there any way to accomplish using Data Services / OData? I know I could probably hack it with a Service Operation but I would prefer not to do that.

干杯.

推荐答案

当前OData(基础协议)不支持Contains操作.这就是为什么客户端库不翻译上面的查询的原因. 人们基本上使用两种方法来克服此限制: 1)按照说明使用服务操作. 2)动态构造一个where子句,该子句使用简单的比较将值与数组中的每个项目进行比较.因此,如果数组包含1、2、3,则where将是data.ID == 1 || data.ID == 2 || data.ID == 3 #2解决方案很好,因为它只是客户端更改.缺点是,它仅适用于小型阵列.如果数组包含太多项,则表达式会变得太长,从而导致各种麻烦. #1解决方案没有大小问题,但是您需要在服务器上提供操作.

Currently OData (the underlying protocol) doesn't support the Contains operation. So that's why the client library does not translate the above query. People are basically using two ways to overcome this limitation: 1) Use service operations as you noted. 2) Construct a where clause dynamically which uses simple comparisons to compare the value to each item from the array. So if the array contains 1, 2, 3, the where would be data.ID == 1 || data.ID == 2 || data.ID == 3 The #2 solution is nice because it's a client side only change. The downside is, that it only works for small arrays. If the array contains too many items the expression gets too long and that leads to all kinds of troubles. The #1 solution doesn't have the size problem, but you need to provide the operation on the server.

这篇关于选择int数组中ID为ID的实体-WCF Data Services,LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆