日期:2014-05-20  浏览次数:20947 次

linq查询条件:某字段内容是否在一个“数组”当中
本帖最后由 fukai22 于 2012-11-29 18:45:05 编辑
我要查个列表,需要判断其id是否在传递进来的“数组”参数当中

例如数组 string[] ids= "3,14,15,19,20".ToString().Split(',');

public List<table> getlist(string[] ids)
{
.......
var list = dc.table.where(a=>a.id.ToString() in ids).ToList();
return list;
}


我知道这一看就是错的,其中条件:a=>a.id.ToString() in ids我想表达的是id是否在数组ids中,
也就是要取出所有“id在数组ids中存在”的数据列表,求人解答。。。。
------解决方案--------------------
Where(x=>数组.Contains(x)).Select(v=>v)
------解决方案--------------------
dc.table.Where(x => ids.Contains(x));
------解决方案--------------------
var list = dc.table.where(a=>ids.Contains(a)).ToList();
------解决方案--------------------

public class table
    {
        public int id { get; set; }
        public string name { get; set; }
    }


static void Main(string[] args)
        {
            string[] ids = "3,14,15,19,20".ToString().Split(',');

            List<table> tc = new List<table> { new table { id = 3, name = "a" }, new table { id = 14, name = "b" }, new table { id = 3, name = "c" } };
            List<table> tables = tc.Where(a => ids.Contains(a.id.ToString())).ToList();
            tables.ForEach(a => { Console.WriteLine(string.Concat("id=", a.id.ToString())); Console.WriteLine(string.Concat("name=", a.name)); });

            Console.Read();
        }

------解决方案--------------------
var list = dc.table.Where(a=>ids.Contains(a.id.ToString())).ToList();