【问题标题】:How do I use "in" where clause in LINQ when retrieving records/entities in Microsoft CRM?在 Microsoft CRM 中检索记录/实体时,如何在 LINQ 中使用“in”where 子句?
【发布时间】:2014-07-11 01:54:18
【问题描述】:

只是在使用 LINQ 和 Microsoft CRM 时遇到一些问题,希望您能提供帮助。

在 SQL 术语中,我的想法是:

select * from table where id in ("1", "2", "3")

我有这段代码,它在从 SQL Server 检索记录时工作正常。

 var _records = from _adUserDatas in _adUserDataDBDataContex.ADUserDatas
     where
          _list.Contains(_adUserDatas.id)
     orderby _adUserDatas.fan
     select _adUserDatas;

当我将其更改为使用 Microsoft CRM 中的联系人实体时,它会引发异常,并显示我的 where 子句无效。

任何想法如何让它工作?

谢谢

ps。我发现了这个 stackoverflow 讨论,但我在 myset.where 部分遇到了错误。

【问题讨论】:

  • 使用联系人实体的时候报错是什么意思?

标签: c# linq dynamics-crm-2011 microsoft-dynamics


【解决方案1】:

CRM LINQ 上下文不支持这一点。请在此处查看LINQ limitations

LINQ 运算符: 在哪里

限制: 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.

有一个名为Dynamic Linq 的解决方法。看看能不能帮到你。

使用Dynamic Linq,类似以下的内容应该会为您工作。

var adUserDatas = _adUserDataDBDataContex.ADUserDatas
string[] _list = new[] { "1", "2", "3" };
string whereClause = String.Empty;

foreach (var id in _list)
{
    whereClause += string.Format("_adUserDatas.id = \"{0}\" OR ", id);
}

adUserDatas = adUserDatas.Where(whereClause.Substring(0, whereClause.Length - 4));

var query = from n in adUserDatas
        select n;

参考:Link

【讨论】:

  • 查询表达式包含一个In 运算符,因此您可以随时使用它,而不是动态链接
  • @Daryl 我认为这是一个不错的选择,感谢您告诉我。
猜你喜欢
  • 1970-01-01
  • 2018-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-23
  • 1970-01-01
相关资源
最近更新 更多