dla*_*n77 7 linq microsoft-dynamics crm dynamics-crm dynamics-crm-2011
我有一个名为new_trexmail的实体,其中包含一个名为new_contextline的字符串属性.
我正在尝试获取一个实体列表,其中new_contextlineis在定义的列表中.
以下代码失败并显示错误: NotSupportedException: Invalid 'where' condition. An entity member is invoking an invalid property or method.
string[] test = new[]{"aaa", "hhh"};
var query = from n in New_trexmailSet
where test.Contains(n.New_contextline)
select n;
Run Code Online (Sandbox Code Playgroud)
我理解为什么会抛出这个错误,但我想知道是否可以使用XRM进行IN子句的等效.
如果可能的话,我该如何让XRM执行SELECT * FROM new_trexmail WHERE new_contextline in ('aaa', 'hhh')?
谢谢,
大卫
查看(长于预期的)LINQ限制列表,特别是对该where子句的限制:
子句的左侧必须是属性名称,子句的右侧必须是值.您不能将左侧设置为常量.该子句的两个边都不能是常量.支持String函数Contains,StartsWith,EndsWith和Equals.
因此,由于test不是CRM属性,您无法调用Contains它.然而,解决这个问题的一种方法是使用由ScottGu开发的" Dynamic Linq ",如下所示:
//must include the below using statements
//using System.Linq;
//using System.Linq.Dynamic;
var trexmailSet = New_trexmailSet;
string[] test = new[] { "aaa", "hhh" };
string whereClause = "";
foreach (string name in test)
{
whereClause += string.Format("new_contextline = \"{0}\" OR ", name);
}
trexmailSet = trexmailSet.Where(whereClause.Substring(0, whereClause.Length - 4));
var query = from n in trexmailSet
select n;
Run Code Online (Sandbox Code Playgroud)