简体   繁体   English

LINQ'查询语法'—根据int列表过滤where子句

[英]LINQ 'query syntax' — where clause filtering against List of int

Struggling on how to get this filter to work: 努力使此过滤器正常工作:

  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(); 

Getting this error message: 收到此错误消息:

Unable to cast the type 'System.Int32' to type 'System.Object'. 无法将类型“ System.Int32”强制转换为类型“ System.Object”。 LINQ to Entities only supports casting EDM primitive or enumeration types LINQ to Entities仅支持强制转换EDM基本类型或枚举类型

You are are trying to compare a List<int> to an int , which won't work. 您正在尝试将List<int>int进行比较,这是行不通的。

If you're looking to get all contacts that are in your hard coded List<int> , just use the Contains method. 如果要获取硬编码List<int>所有联系人,只需使用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(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM