Dynamically Construct Linq in CRM 2011

戏子无情 提交于 2019-12-07 06:53:29

CRM reads the created Linq statement and is very particular. This should work for you since the set is of type IQueryable, you can add your where statements as needed:

public void GetLock(bool account = false, bool contact = false, bool incident = false)
{
    var query = linq.de_processimportSet;

    if(account){
        query = query.Where(p => p.de_ProcessingAccount);
    }

    if(contact){
        query = query.Where(p => p.de_ProcessingContact);
    }

    if(incident){
        query = query.Where(p => p.de_ProcessingIncident);
    }

    var processing = query.ToList();
}

Edit

CRM's Linq doesn't support this out of the box, but you can download LinqKit and use it's predicate builder and AsExpandable magic to make it work. Check out a similar example here

You could also abandon Linq and use Query Expressions in this case:

public void GetLock(bool account = false, bool contact = false, bool incident = false)
{
    var qe = new QueryExpression("de_processimport");
    qe.ColumnSet = new ColumnSet(true);

    if(account){
        qe.AddCondition("de_processingaccount" ConditionOperator.Equal, true);
    }

    if(contact){
        qe.AddCondition("de_processingcontact" ConditionOperator.Equal, true);
    }

    if(incident){
        qe.AddCondition("de_processingincident" ConditionOperator.Equal, true);
    }

    var processing = service.RetrieveMultiple(qe).Entities.Select(c => c.ToEntity<de_processimport>());
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!