2011-01-25 98 views
2

我有一个客户id列表,custList(的字符串)。LINQ to Dataset - 相当于sql(where ... in ...)

我想对数据集使用LINQ查询来获取客户表中的所有客户,其中customerID是“我”custList(字符串)。

这在LINQ中可能吗?我在网上搜索并没有找到答案。我是新来的LINQ ..

感谢

+0

感谢所有为你的速度回应!我结束了使用︰Dim query1 = From record In ds.Tables(“Customers”)。AsEnumerable()_ Where lst.Contains(record.Field(Of String)(“custID”)) Select record – novice 2011-01-25 13:00:50

+0

You're欢迎。如果可以的话,请务必提供有用的答案并标记解决问题的答案。 – 2011-01-25 16:23:13

回答

1
var custList = new HashSet<string>() { "a", "b", "c"...}; 

from record in table.ToEnumerable() 
where custList.Contains(record.Field<string>("customerID")) 
2

在LINQ使用Contains()方法来执行这些类型的查询。

我不知道LINQ到数据集,但在LINQ to SQL中,你可以做到以下几点:

var statuses = new int[] {1, 2, 3}; 

var query = from p in dataContext.Products 
      where statuses.Contains(p.Id) 
      select p; 

这应该产生类似于SQL:

select * from Product p 
where p.Id in (1, 2, 3) 

(注意如何在LINQ代码中对生成的SQL进行背对背 - 这就是为什么如果你很熟悉SQL,它不是很直观,但它可以非常优雅地使用现有的.NET语言特性)

这通常也适用于string以及L2S知道的其他一些基本类型的集合,因为它们在框架中。

0
var custList = new HashSet<int> { 10, 15, 17 }; 
CustomerSet.Where(c => custList.Contains(c.CustomerID));