Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linq select from database where ID in an ArrayList

Tags:

arraylist

linq

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?

like image 587
Moslem Hady Avatar asked Aug 11 '11 12:08

Moslem Hady


People also ask

Can LINQ query work with array?

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.

Can we use LINQ on ArrayList in C#?

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#.

What is the difference between where and Select in LINQ?

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.

Does LINQ Select return new object?

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.


1 Answers

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<>.

like image 124
Jon Skeet Avatar answered Sep 23 '22 12:09

Jon Skeet