C# LINQ到Dynamics CRM查询本地筛选记录

C# LINQ到Dynamics CRM查询本地筛选记录,c#,linq,dynamics-crm,dynamics-crm-2011,C#,Linq,Dynamics Crm,Dynamics Crm 2011,我已经使用CRM 2011 RC(v5)Linq to CRM provider编写了一个Linq to CRM查询。我有一个本地声明的列表,我希望将其加入到CRM实体,并且我希望在CRM服务器上执行查询。举个例子可能会有所帮助: MyObject myObject = new MyObject(); List<myAccount> myAccountsList = new List<myAccount>(); myAccountsList.Add(new myAcco

我已经使用CRM 2011 RC(v5)Linq to CRM provider编写了一个Linq to CRM查询。我有一个本地声明的列表,我希望将其加入到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());
}
或者,如果要在多个字段上联接,则使用临时表。我怎样才能做到这一点呢?

编辑:试试那个:

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 accountNumbers = myObject.ListOfAccounts.Select(a => a.AccountNumber);

var accountsQuery = orgContext.CreateQuery<customAccountEntity>()
                              .Where(a => accountNumbers.Contains(a.account_number));

foreach(var item in accountsQuery)
{
    Console.WriteLine("Id of record retrieved: " + a.Id.ToString());
}
MyObject MyObject=new MyObject();
List myAccountsList=新列表();
添加(新的myAccount(){AccountNumber=“123”};
添加(新的myAccount(){AccountNumber=“456”};
myObject.ListOfAccounts=myAccountsList;
var accountNumbers=myObject.ListOfAccounts.Select(a=>a.AccountNumber);
var accountsQuery=orgContext.CreateQuery()
其中(a=>accountnumber.Contains(a.account_number));
foreach(accountsQuery中的var项目)
{
Console.WriteLine(“检索到的记录Id:+a.Id.ToString());
}

编辑:如果查询提供程序不支持Contains,请使用多个OR构建一个Where条件,您可以使用使其变得简单

在进行了大量的讨论和研究后,我已使用and解决了问题。我需要使用本地列表中的键创建一个基于OR的谓词,然后将谓词传递给Where LINQ扩展方法。I重要的是,我需要调用LINQKit公开的AsExpandable扩展方法。因此,我的代码如下所示:

SELECT a.*
FROM customAccountEntity AS a
WHERE a.account_number IN ('123', '456');
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);
}
SELECT a.*
FROM customAccountEntity AS a
WHERE a.account_number = '123' OR a.account_number = '456'

这意味着我可以在运行时创建一个动态where子句,并且知道我的查询将在CRM SQL Server上运行筛选逻辑。希望这对其他人有所帮助。

除了使用谓词,您还可以简单地使用联接表达式进行筛选

var myLinqToCrmQuery =  from ax in myObject.ListOfAccounts
                            join cx in orgContext.CreateQuery<customAccountEntity> on ax.AccountNumber equals cx.account_number                      
                            select cx;
var myLinqToCrmQuery=来自myObject.listofcounts中的ax
在ax上的orgContext.CreateQuery中加入cx。AccountNumber等于cx.account\U number
选择cx;
干杯,
Lukasz

我已经在使用Linq to Crm查询提供程序-我可以在同一个查询中使用多个提供程序吗?在仔细查看您的查询后,我认为您应该以另一种方式进行操作(无连接,但包含)我希望将我的查询转换为SQL,并在SQL Server上运行筛选。Contains扩展方法只是在本地进行筛选。Contains可以转换为SQL中的IN语句,并且您可以使用本地对象(ListOfAccounts),这是使用LinqPad获得IMHOexample的最好方法:var id=new[]{1,2,3};var query=Users。其中(u=>ids.Contains(u.Id));转换为:从[User]中选择[t0].[Id],[t0].[Email],[t0].[Password]作为[t0],其中[t0].[Id]在(@p0,@p1,@p2)中,我希望在服务器上运行查询。使用联接表达式,过滤在客户端本地完成。