PredicateBuilder and LINQ to CRM

本文关键字:CRM to LINQ and PredicateBuilder | 更新日期: 2023-09-27 18:09:05

我试图用许多谓词拉出已经运行的工作流的所有实例(从AsyncOperationBase表)。

我知道你不能使用像myArray.Contains(x.WorkflowActivationId.Id)这样的东西,所以我试图用PredicateBuilder动态地建立一个谓词

到目前为止我写的是:

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();

但是我最终得到了一个非常LINQ到CRM的可怕的例外:

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

据我所知,为了不得到这个错误,你需要"爬下"属性。比如:

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

必须改为

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

而且,谓词的左边必须是一个实体属性。再一次,据我所知,我的谓词满足这些要求,所以我在这里有点茫然。

有什么想法吗?

PredicateBuilder and LINQ to CRM

我认为我需要通过做这两部分来绕过LINQ对CRM的限制。

1:

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();

我现在只需要这两个属性。

然后我将能够有完整的LINQ访问t的内容。

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