WCF Data Services: request by multiple parameters
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
开发者_StackOverflow社区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));
}
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.
精彩评论