LINQ to Dataset - equivalent of sql (where ... in...)
I have a list of customer id's, custList(of string).
I want to use a LINQ query to a Dataset, to get all customers in the customer table, where customerID is "IN" my custList(of string).
Is this possible in LINQ? I have search开发者_如何学Pythoned online and not found the answer. I'm new to LINQ..
Thanks
In LINQ you use the Contains()
method to perform these kind of queries.
I don't know LINQ to DataSets, but in LINQ to SQL you can do the following:
var statuses = new int[] {1, 2, 3};
var query = from p in dataContext.Products
where statuses.Contains(p.Id)
select p;
This should generate SQL similar to:
select * from Product p
where p.Id in (1, 2, 3)
(Note how it feels back-to-front in the LINQ code to the generated SQL - that's why if you know SQL well it's not very intuitive, but it makes very elegant use of existing .NET language features)
This also typically works for string
and collections of some other basic types that L2S knows about because they're in the framework.
var custList = new HashSet<string>() { "a", "b", "c"...};
from record in table.ToEnumerable()
where custList.Contains(record.Field<string>("customerID"))
var custList = new HashSet<int> { 10, 15, 17 };
CustomerSet.Where(c => custList.Contains(c.CustomerID));
精彩评论