可以在Dynamics CRM中使用LINQ来获取不在集合中的所有帐户吗? [英] Can LINQ be used in Dynamics CRM to get all Accounts not in a Collection?

查看:76
本文介绍了可以在Dynamics CRM中使用LINQ来获取不在集合中的所有帐户吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编写LINQ查询以返回帐号不在列表中的所有帐户?
该列表将从Excel文档中提取.

How can a LINQ query be written to return all Accounts where the account number is not in a List?
The list is going to be pulled from an excel document.

private bool GetAccounts()
{
        List<String> accountIds = new List<String>();
        accountIds.Add( "[unknown]");
        var query = from accounts in context.AccountSet where !accountIds.Contains(accounts.AccountNumber) select accounts;
}

它不一定是列表.

编辑

运行上述查询时会发生什么-这是CRM的错误吗?

This is what happens when the above query runs - Is this CRM's fault?

推荐答案

我不相信您可以通过linq.这是SDK的where子句限制.

I don't believe you can via linq. Here is the where clause limitations from the SDK.

其中=> 子句的左侧必须是属性名称,子句的右侧必须是值.您不能将左侧设置为常数.子句的两面都不能为常数.

where => The left side of the clause must be an attribute name and the right side of the clause must be a value. You cannot set the left side to a constant. Both the sides of the clause cannot be constants.

支持包含,StartsWith,EndsWith和Equals的String函数.

Supports the String functions Contains, StartsWith, EndsWith, and Equals.

您可以使用QueryExpression或FetchExpressions来解决这些限制.使用QueryExpression,所需的查询看起来像这样.我唯一要提及的是,如果您希望获得大量记录(我相信有5000多个记录),那么您很可能也需要为函数实现分页.

You can get around these limitations by using QueryExpression or FetchExpressions. The query you want would look like this using QueryExpression. The only thing I would mention is if you are expecting a lot of record (5000+ I believe) you will most likely need to implement paging for your function as well.

private static IEnumerable<Account> GetAccounts(IOrganizationService proxy)
{
    List<String> accountIds = new List<String>(new string[]{"654321", "12345"});

    var results = proxy.RetrieveMultiple(new QueryExpression(Account.EntityLogicalName)
    {
        ColumnSet = new ColumnSet("accountid", "name", "accountnumber"),
        Criteria = new FilterExpression()
        {
            Conditions = { new ConditionExpression("accountnumber", ConditionOperator.NotIn, accountIds) }
        }
    });

    return results.Entities.Select(x=> x.ToEntity<Account>());
}

这篇关于可以在Dynamics CRM中使用LINQ来获取不在集合中的所有帐户吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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