简体   繁体   中英

PredicateBuilder and LINQ to CRM

I'm trying to pull all the instances of a workflow that have ran (from the AsyncOperationBase table) with a number of predicates.

I know you can't use something like myArray.Contains(x.WorkflowActivationId.Id) so I'm trying to build a predicate dynamically with PredicateBuilder

Here's what I have so far:

var predicate = PredicateBuilder.False<Service.AsyncOperation>();

//Apparently, checking for null is suppose to prevent my issue from happening...
predicate = predicate.And(x => x.Name == searchWorkflowDefinitionText.Text);
predicate = predicate.And(x => x.StatusCode != null);
predicate = predicate.And(x => x.StatusCode.Value == 10);
predicate = predicate.And(x => x.WorkflowActivationId != null);


foreach (Guid s in listBox3.Items)
{
    predicate = predicate.Or(x => x.WorkflowActivationId.Id == s);
}


var r = (from c in xrm.CreateQuery<Service.AsyncOperation>().AsExpandable().Where(predicate)
                         select c).ToList();

But I end up getting the much LINQ to CRM dreaded exception:

Invalid 'where' condition. An entity member is invoking an invalid property or method.

As far as I know, to not get this error, you need to 'crawl down' attributes. Things like:

predicate = predicate.Or(x => x.WorkflowActivationId == s);

Have to be changed to

predicate = predicate.Or(x => x.WorkflowActivationId.Id == s);

And also, the left hand side of the predicate must be a entity attribute. Again, as far as I know, my predicate satisfies those requirements so I am a bit at a loss here.

Any ideas ?

I think I will need to circumvent LINQ to CRM's limits by doing this 2 part.

1st:

var t = xrm.AsyncOperationSet.Where(x => x.StatusCode.Value == 10 && x.Name == wkname).Select(y => new Service.AsyncOperation
                {
                    AsyncOperationId = y.AsyncOperationId.Value,
                    WorkflowActivationId = y.WorkflowActivationId
                }).ToList();

I really need only these two attributes for the moment.

Then I'll be able to have full LINQ access to t 's content.

var y = t.Where(x => myArray.Contains(x.WorkflowActivationId.Id)).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM