我有一个OData服务,我试图通过ID列表过滤; SQL等价物将是这样的:
SELECT * FROM MyTable WHERE TableId IN (100, 200, 300, 400)
Run Code Online (Sandbox Code Playgroud)
我想要过滤的属性是作为Int32键入的.我尝试了以下内容,它给出了一个错误"运算符'添加'与操作数类型'Edm.String'和'Edm.Int32'不兼容":
string ids = ",100,200,300,400,";
from m in provider.Media where ids.Contains("," + t.media_id + ",")
Run Code Online (Sandbox Code Playgroud)
以及
string ids = ",100,200,300,400,";
from m in provider.Media where ids.Contains("," + t.media_id.ToString() + ",")
Run Code Online (Sandbox Code Playgroud)
和
string ids = ",100,200,300,400,";
from m in provider.Media where ids.Contains("," + Convert.ToString(t.media_id) + ",")
Run Code Online (Sandbox Code Playgroud)
和
string ids = ",100,200,300,400,";
from m in provider.Media where ids.Contains(string.Concat(",", t.media_id, ","))
Run Code Online (Sandbox Code Playgroud)
如您所见,目前我正在使用LINQ来查询服务.
有没有办法,我可以做我正在尝试的,或者我是不是构建文本过滤器和使用AddQueryOption,并迭代列表并手动添加"或media_id eq 100"条款?
OData 的限制(此处列出)阻止我向来自我的 OData 源的数据集添加动态 where 子句。我发现以前的帖子回答了我对动态过滤器的查询,即使用 AddQueryOption 方法和自定义构建的查询字符串。但是,似乎没有办法将此查询字符串与标准 LINQ 查询结合起来。
使用上述方法产生一个有效的查询:
https://api-dev.company.com/odata/Assets?$filter=(Levels/any(l:l/LevelId eq 18)) or (Levels/any(l:l/LevelId eq 19))
Run Code Online (Sandbox Code Playgroud)
必须动态生成的原因是因为在运行时之前无法确定可变数量的级别过滤器,并且只需使用多个 Where 子句即可生成“and”过滤器而不是“or”过滤器,如下所示:
https://api-dev.company.com/odata/Assets?$filter=(Levels/any(l:l/LevelId eq 18)) and (Levels/any(l:l/LevelId eq 19))
Run Code Online (Sandbox Code Playgroud)
在此方法产生以下输出后,我当前尝试使用 LINQ:
https://api-dev.company.com/odata/Assets?$filter=DisplayOnline and Status eq Tools.Services.Models.EPublishStatus'Active', and (Levels/any(l:l/LevelId eq 18)) or (Levels/any(l:l/LevelId eq 19))
Run Code Online (Sandbox Code Playgroud)
请注意,第二个查询的唯一错误是 Levels 过滤器和其余过滤器之间的逗号。
附加的 Where 子句如下:
// Filter by assets that can be displayed online
assets = assets.Where(a => a.DisplayOnline);
// Filter by assets that are active
assets = assets.Where(a => a.Status == …Run Code Online (Sandbox Code Playgroud)