我有一个名为new_trexmail的实体,其字符串属性名为new_contextline.
我正在尝试获取一个实体列表,其中new_contextlineis位于已定义的列表中.
以下代码失败并显示错误:NotSupportedException:无效的“where”条件.实体成员正在调用无效的属性或方法.
string[] test = new[]{"aaa","hhh"}; var query = from n in New_trexmailSet where test.Contains(n.New_contextline) select n;
我理解为什么会抛出这个错误,但我想知道是否可以使用XRM进行IN子句的等价.
如果有可能那么我如何让XRM执行SELECT * FROM new_trexmail WHERE new_contextline in(‘aaa’,’hhh’)?
谢谢,
大卫
解决方法
检查(超过期望的)
list of LINQ limitations,特别是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. Supports
the String functions Contains,StartsWith,EndsWith,and 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;