【问题标题】:LINQ 'query syntax' -- where clause filtering against List of intLINQ 'query syntax' -- where 子句对 int 列表进行过滤
【发布时间】:2014-08-20 00:39:43
【问题描述】:

苦于如何让这个过滤器工作:

  var categoryIDs = new List<int>();
              categoryIDs.Add(2);
              categoryIDs.Add(3);

             var dbContacts = (from cnt in _db.Contacts
                join ucc in _db.UserContactCategories on cnt.id equals ucc.ContactID
                join cat in _db.Categories on ucc.CatDescID equals cat.id
                where categoryIDs.Equals(cnt.id)
                select new {cnt.id,
                    cnt.GivenName,
                    cnt.SurName                         
                }).ToList(); 

收到此错误消息:

无法将类型“System.Int32”转换为类型“System.Object”。 LINQ to Entities 仅支持转换 EDM 基元或枚举类型

【问题讨论】:

  • 您正在尝试将List&lt;int&gt;int 进行比较,但这是行不通的。您是否真的在尝试使用等效的 sql IN 运算符?

标签: c# linq


【解决方案1】:

您正在尝试将 List&lt;int&gt;int 进行比较,但这是行不通的。

如果您要获取硬编码List&lt;int&gt; 中的所有联系人,只需使用Contains 方法。

var categoryIDs = new List<int>();
              categoryIDs.Add(2);
              categoryIDs.Add(3);

             var dbContacts = (from cnt in _db.Contacts
                join ucc in _db.UserContactCategories on cnt.id equals ucc.ContactID
                join cat in _db.Categories on ucc.CatDescID equals cat.id
                where categoryIDs.Contains(cat.id)
                select new {cnt.id,
                    cnt.GivenName,
                    cnt.SurName                         
                }).ToList(); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-10
    • 2018-03-10
    • 2013-09-25
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 2011-10-19
    相关资源
    最近更新 更多