OData“,其中列表中的ID”查询 [英] OData "where ID in list" query
问题描述
我有一个OData服务,我想通过ID列表过滤; SQL的等价类似于:
I have an OData service where I'm trying to filter by a list of IDs; the SQL equivalent would be something like:
SELECT * FROM MyTable WHERE TableId IN (100, 200, 300, 400)
我想要过滤的属性是以Int32格式输入的。我试过以下,这给我一个错误操作符'添加'不兼容操作数类型'Edm.String'和'Edm.Int32':
The property I'm trying to filter on is typed as an Int32. I've tried the following, which gives me an error "Operator 'add' incompatible with operand types 'Edm.String' and 'Edm.Int32'":
string ids = ",100,200,300,400,";
from m in provider.Media where ids.Contains("," + t.media_id + ",")
b $ b
以及
as well as
string ids = ",100,200,300,400,";
from m in provider.Media where ids.Contains("," + t.media_id.ToString() + ",")
和
string ids = ",100,200,300,400,";
from m in provider.Media where ids.Contains("," + Convert.ToString(t.media_id) + ",")
和
string ids = ",100,200,300,400,";
from m in provider.Media where ids.Contains(string.Concat(",", t.media_id, ","))
正如你所看到的,目前我正在使用LINQ来查询服务。
As you can see, currently I'm using LINQ to query the service.
有什么方法可以做我试图,或者我坚持构造一个文本过滤器和使用AddQueryOption,并迭代列表,并手动添加或media_id eq 100子句?
Is there a way I can do what I'm trying to, or am I stuck constructing a text filter and using AddQueryOption, and iterating through the list and manually adding "or media_id eq 100" clauses?
推荐答案
试试这个
var ids = new [] { 100, 200, 300 } ;
var res = from m in provider.Media
from id in ids
where m.media_id == id
select m;
有关于 msdn 查询DataServices。
there is a comprehensive description on msdn on querying DataServices.
另一种方法是
var results = provider.Media
.AddQueryOption("$filter", "media_id eq 100");
,因为OData不支持 IN
您将想出过滤条件的语句如下
and since OData doesn't support IN
statements you will come up with filter condition like this
.AddQueryOption("$filter", "(media_id eq 100) or (media_id eq 200 ) or ...");
您可以使用loop或linq建立选择
和 string.Join
:
which you can build using loop or linq Select
and string.Join
:
var ids = new [] { 100, 200, 300 };
var filter = string.Join(" or ", ids.Select(i=> $"(media_id eq {i})"));
var results = provider.Media.AddQueryOption("$filter", filter);
更新:有过滤操作 field = [a,b ]
然而它意味着不同的东西。
UPDATE: There is filter operation field=["a","b"]
however it means something different.
UPDATE2:在OData V4中有lambda表达式任何
和全部
与数组文本 [a,b]
配对,它们可能在中工作,未能在OData.org上提供有关示例v4端点的工作语法。
UPDATE2: In OData V4 there is lambda expressions any
and all
, paired with array literal ["a", "b"]
they might work as in
but I failed to come up with working syntax on example v4 endpoint at OData.org
这篇关于OData“,其中列表中的ID”查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!