Moslem Hadi
Moslem Hadi

Reputation: 1020

linq select from database where ID in an ArrayList

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?

Upvotes: 13

Views: 16544

Answers (3)

Moslem Hadi
Moslem Hadi

Reputation: 1020

List<long> list =new List<long>(); 
var selected = from n in users where list.Contains(n.ID) select n ;

OR

var selected = users.Where(a=> list.Contains(a.ID)).ToList();

Upvotes: 6

Nitesh
Nitesh

Reputation: 852

This is the solution I used.

public static IEnumerable<SettingModel> GetSettingBySettingKeys(params string[] settingKey)
    {
        using (var db = new BoxCoreModelEntities())
        {
            foreach (var key in settingKey)
            {
                var key1 = key;
                yield return Map(db.Settings.Where(s => s.SettingKey == key1).First());
            }
        }
    }

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1502006

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

Upvotes: 21

Related Questions