I have an array-list that contains some UserID. I need a query like this:
vat tmp= users.select(a=> a.UserID in (arraylist));
what can I do?
Yes it supports General Arrays, Generic Lists, XML, Databases and even flat files. The beauty of LINQ is uniformity. Now, how does it provide uniformity? Using a single LINQ query you can process data in various data sources.
LINQ with ArrayList You don't need to specify array size unlike traditional array and the size grows as you will add more element into it. However, it is slower than Array but it is more useful when you work with collection. Here, in this example, we will learn how to process data in ArrayList using LINQ C#.
Select maps an enumerable to a new structure. If you perform a select on an IEnumerable, you will get an array with the same number of elements, but a different type depending on the mapping you specified. Where filters the IEnumerable so that it gives you a subset of the original IEnumerable.
While the LINQ methods always return a new collection, they don't create a new set of objects: Both the input collection (customers, in my example) and the output collection (validCustomers, in my previous example) are just sets of pointers to the same objects.
If it's actually in an ArrayList
, you should create a List<T>
or array first. Then you can use Contains
:
// Use the appropriate type, of course.
var ids = arraylist.Cast<string>().ToList();
var tmp = users.Select(a => ids.Contains(a.UserID));
While using Contains
on the plain ArrayList
may well compile, I would expect it to fail at execution time, assuming users
is an IQueryable<>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With