如何使用&;in&;在Microsoft CRM中检索记录/实体时,LINQ中的where子句

本文关键字:实体 LINQ where 子句 中的 检索 in 何使用 Microsoft CRM 记录 | 更新日期: 2023-09-27 18:12:20

我在使用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子句无效

有什么办法让它工作吗?

感谢

p。我发现这个stackoverflow讨论,但我在myset上得到错误。一部分。

如何使用&;in&;在Microsoft CRM中检索记录/实体时,LINQ中的where子句

CRM LINQ Context不支持此操作。请在这里查看LINQ限制。

LINQ Operator: 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.

有一个变通方法叫做动态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;

裁判:链接