加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

linq – 相当于SQL IN子句

发布时间:2020-12-12 16:21:53 所属栏目:MsSql教程 来源:网络整理
导读:我有一个名为new_trexmail的实体,其字符串属性名为new_contextline. 我正在尝试获取一个实体列表,其中new_contextlineis位于已定义的列表中. 以下代码失败并显示错误:NotSupportedException:无效的“where”条件.实体成员正在调用无效的属性或方法. string[
我有一个名为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;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读