日期:2014-05-20 浏览次数:20779 次
var ids = new List<int> { 6, 8, 9, 10 }; var query = from x in ctx.SomeTable where ids.Contains(x.Id) select x;
------解决方案--------------------
var listOfIds= new int[]{5,6,7,8};
var context=CreateEntityFrameworkObjectContext();
var results = from item in context.Items
where listOfIds.Contains(item.Category.Id)
select item;
------解决方案--------------------
大致原理如下,逆向实现。
int[] idset = new int[] { 5, 6, 7, 8 }; var result = mydatarows.Where(r=>idset.Contains(r.id));
------解决方案--------------------
var listOfIds= new string[]{"5","6","7","8"};
var context=CreateEntityFrameworkObjectContext();
var results = from item in context.Items
where listOfIds.Contains(item.Category.Id.ToString())
select item;
------解决方案--------------------
实测结果如下,尽管LINQ to Entity与LINQ to SQL Entity有一些差异,在有比较子的运算符上可能需要自己实现,不过你这里的需求应该是可以直接实现的。
using (NorthwindEntities db = new NorthwindEntities()) { string[] cities = new string[] { "London", "Paris", "Berlin" }; var result = db.Customers.Where(r => cities.Contains(r.City)); foreach (var r in result) { Console.WriteLine(r.ContactName); } }