LINQ to Dynamics CRM查询本地过滤记录

Ift*_*har 12 c# linq dynamics-crm dynamics-crm-2011

我使用CRM 2011 RC(v5)LINQ-to-CRM提供程序编写了一个Linq to CRM查询.我有一个本地声明的List <T>,我想加入CRM实体,我希望查询在CRM服务器上执行.一个例子可能有帮助:

MyObject myObject = new MyObject();
List<myAccount> myAccountsList = new List<myAccount>();

myAccountsList.Add(new myAccount() {AccountNumber = "123"};
myAccountsList.Add(new myAccount() {AccountNumber = "456"};

myObject.ListOfAccounts = myAccountsList;

var accountsQuery = from ax in myObject.ListOfAccounts
                    join a in orgContext.CreateQuery<customAccountEntity>() on ax.AccountNumber equals a.account_number
                    select a;

foreach(var item in accountsQuery)
{
    Console.WriteLine("Id of record retrieved: " + a.Id.ToString());
}
Run Code Online (Sandbox Code Playgroud)

上面的代码编译并执行,但是,在检索整个CRM实体记录集之后,本地执行记录的过滤.显然,当CRM实体包含数千行时,查询将执行不佳甚至超时.

我已阅读有关IQueryable和IEnumerable的内容,并尝试使用AsQueryable()扩展方法转换List,该方法无效.我需要上面的Linq查询来运行SQL,如下所示:

SELECT a.*
FROM customAccountEntity AS a
WHERE a.account_number IN ('123', '456');
Run Code Online (Sandbox Code Playgroud)

如果想要加入多个字段,可以使用临时表.我怎么能做到这一点?

Ift*_*har 13

经过大量的敲击和研究后,我通过使用Predicate BuilderLINQKit解决了这个问题.我需要使用本地List <T>中的创建一个基于Or的谓词,然后将谓词传递给Where LINQ扩展方法.重要的是,我需要调用LINQKit公开的AsExpandable扩展方法.所以我的代码看起来像这样:

var predicate = PredicateBuilder.False<customAccountEntity>();
// Loop through the local List creating an Or based predicate
foreach (var item in myAccountsList)
{
    string temp = item.AccountNumber;
    predicate = predicate.Or (x => x.customCrmEntityAttribute == temp);
}
// The variable predicate is of type Expression<Func<customAccountEntity, bool>>
var myLinqToCrmQuery =  from ax in myObject.ListOfAccounts
                        from cx in orgContext.CreateQuery<customAccountEntity>().AsExpandable().Where(predicate)
                        where ax.AccountNumber == cx.account_number
                        select cx;

foreach (resultItem in myLinqToCrmQuery)
{
    Console.WriteLine("Account Id: " + resultItem.Id);
}
Run Code Online (Sandbox Code Playgroud)

上面的代码将在CRM服务器上运行SQL语句,如下所示:

SELECT a.*
FROM customAccountEntity AS a
WHERE a.account_number = '123' OR a.account_number = '456'
Run Code Online (Sandbox Code Playgroud)

这意味着我可以在运行时创建一个动态where子句,并知道我的查询将在CRM SQL Server上运行过滤逻辑.希望这有助于其他人.