WCF数据服务:通过多个参数请求
我正在尝试生成对我的 WCF 服务的请求。 我需要通过日期时间参数或 ID 查询 WCF。
我的意思是在最终结果中我想得到像 myservice.svc/MyObjects()?$filter=CreateDate ge datetime'datecomeshere' 或 Id eq 3 或 Id eq 45 或 Id eq 112
问题是我在客户端有一组 ids 和一个日期变量。 那么如何向 WCF 生成这种请求呢?
这是我现在拥有的代码:
var localEntityIds = DbConnection.ObjectContextConnection.GetEntitiesByDate<T>(DateToReplicate).Select(x => x.Id);
if (DateToReplicate > DateTime.MinValue)
{
expQuery =
Service.CreateQuery<T>(SetName).Where(
x => x.ReplicaInfo.CreateDate >= DateToReplicate ||
x.ReplicaInfo.ModifyDate >= DateToReplicate || localEntityIds.Where(y=>y ==x.Id).Any()) as DataServiceQuery<T>;
}
此代码抛出一个异常,最大协议版本应不低于 3.0 并且不支持任何方法。 但我有 3.0 版本,
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
我也尝试通过实现此代码来解决此问题,但在这种情况下 wcf 不返回任何内容:
var localentities = DbConnection.ObjectContextConnection.GetEntitiesByDate<T>(DateToReplicate).Select(x => x.Id);
if (DateToReplicate > DateTime.MinValue)
{
expQuery =
Service.CreateQuery<T>(SetName).Where(
x => x.ReplicaInfo.CreateDate >= DateToReplicate ||
x.ReplicaInfo.ModifyDate >= DateToReplicate) as DataServiceQuery<T>;
}
foreach (var localentity in localentities)
{
expQuery = expQuery.AddQueryOption("or", string.Format("Id eq guid'{0}'", localentity));
}
I'm trying to generate a request to my WCF service.
I need to query WCF by datetime parameter OR by Ids.
I mean in the final result I want to get the url like
myservice.svc/MyObjects()?$filter=CreateDate ge datetime'datecomeshere' or Id eq 3 or Id eq 45 or Id eq 112
The problem is that I have a collection of ids on client side and one date variable.
So how can I generate this kind of request to WCF?
Here is the code that I have right now:
var localEntityIds = DbConnection.ObjectContextConnection.GetEntitiesByDate<T>(DateToReplicate).Select(x => x.Id);
if (DateToReplicate > DateTime.MinValue)
{
expQuery =
Service.CreateQuery<T>(SetName).Where(
x => x.ReplicaInfo.CreateDate >= DateToReplicate ||
x.ReplicaInfo.ModifyDate >= DateToReplicate || localEntityIds.Where(y=>y ==x.Id).Any()) as DataServiceQuery<T>;
}
This code throws an exception that max protocol version should be not less than 3.0 and Any method is not supported.
But I have version 3.0
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
I've also tryed to solve this issue implementing this code, but wcf return nothing in this case:
var localentities = DbConnection.ObjectContextConnection.GetEntitiesByDate<T>(DateToReplicate).Select(x => x.Id);
if (DateToReplicate > DateTime.MinValue)
{
expQuery =
Service.CreateQuery<T>(SetName).Where(
x => x.ReplicaInfo.CreateDate >= DateToReplicate ||
x.ReplicaInfo.ModifyDate >= DateToReplicate) as DataServiceQuery<T>;
}
foreach (var localentity in localentities)
{
expQuery = expQuery.AddQueryOption("or", string.Format("Id eq guid'{0}'", localentity));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将整个 where 条件构建为字符串并使用
AddQueryOption("$filter", yourBuiltQueryString)
。据我所知,您不能仅添加条件的一部分 - 添加的查询选项的名称必须以$
开头,这只是定义的一组运算符。因此,您不能在查询中使用Where
条件 - 必须通过AddQueryOption
方法调用添加整个条件以构建正确的or
序列。如果是
Any
,请确保您使用的是正确的 CTP(顺便说一句。CTP 不是生产就绪版本)。它已添加到 2011 年 6 月 CTP 并且您必须在服务器和客户端上使用该库。Build whole where condition as a string and use
AddQueryOption("$filter", yourBuiltQueryString)
. As I know you cannot add only part of condition - name of added query option must start with$
which is only defined set of operators. Because of that you cannot useWhere
condition in your query - whole condition must be added byAddQueryOption
method call to build corrector
sequence.In case of
Any
make sure that you are using correct CTP (btw. CTP is not production ready release). It was added in June 2011 CTP and you must use that library on both server and client.