在CRM 2011中动态构建Linq [英] Dynamically Construct Linq in CRM 2011

查看:62
本文介绍了在CRM 2011中动态构建Linq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Linq检索实体的所有记录,条件是该参数中为true的属性之一在记录中为true。



我我正在尝试实现一个逻辑或,请看一下代码,因为它在这里会更有意义。



在FetchXML中,它的工作原理是:

I am trying to retrieve all records of an entity with Linq with the condition that one of the properties that are true in the parameter are true in the record.

I am trying to achieve a logical 'or', please look at the code as it will make more sense there.

In FetchXML it is working:

public void GetLock(bool account = false, bool contact = false, bool incident = false)
{
    var fetch = @"
                <fetch mapping='logical'>
                    <entity name='de_processimport'>
                        <attribute name='de_processimportid' />
                            <filter type='or'>";
    if (account)
        fetch += @"<condition attribute='de_processingaccount' operator='eq' value='true' />";
    if (contact)
        fetch += @"<condition attribute='de_processingcontact' operator='eq' value='true' />";
    if (incident)
        fetch += @"<condition attribute='de_processingincident' operator='eq' value='true' />";
    fetch += @" 
                </filter>
            </entity>
        </fetch>";
    var processing = service.RetrieveMultiple(new FetchExpression(fetch));
    ...
}

在Linq中不起作用:

In Linq it is not working:

public void GetLock(bool account = false, bool contact = false, bool incident = false)
{
    var processing = linq.de_processimportSet
        .Where(p => // get records that are processing the entities we are processing
        (account) ? p.de_ProcessingAccount == true : false // get processImport records where processingAccount is true or otherwise ignore this clause (false)
        || (contact) ? p.de_ProcessingContact == true : false
        || (incident) ? p.de_ProcessingIncident == true : false
        );
    ...
}


推荐答案

CRM读取创建的Linq语句,这非常特殊。这对您应该有用,因为集合的类型为IQueryable,可以根据需要添加where语句:

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



编辑



CRM的Linq不支持此功能,但是您可以下载LinqKit并使用它的谓词生成器和 AsExpandable 魔术来使其正常工作。 检查

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

在这种情况下,您也可以放弃Linq并使用查询表达式:

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

这篇关于在CRM 2011中动态构建Linq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆