WCF数据服务:通过多个参数请求

发布于 2024-12-03 11:21:56 字数 1731 浏览 0 评论 0原文

我正在尝试生成对我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

上课铃就是安魂曲 2024-12-10 11:21:56

将整个 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 use Where condition in your query - whole condition must be added by AddQueryOption method call to build correct or 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文