Linq - Using array in Lambda expression to fetch multiple records

10,840

It's possible, and will translate into a SQL WHERE IN (...) statement, but it is written kind of backwards in linq:

DbDataContext db = new DbDataContext();    
int[] userIds = {15850, 15858};
var users = db.tblUsers.Where(x => userIds.Contains(x.UserId));
Share:
10,840
Eric Herlitz
Author by

Eric Herlitz

System developer, architect and Optimizely MVP that fancies Optimizely/Episerver, Azure, SharePoint and more. Also Co-founder of Bendsoft and the SharePoint .NET Connector. https://twitter.com/EricHerlitz http://www.herlitz.io

Updated on June 09, 2022

Comments

  • Eric Herlitz
    Eric Herlitz almost 2 years

    Im not sure wether this is possible or not. I'd like to create an array (or list/dictionary) containing some simple id's and the use the array (or whatever) in a lambda expression.

    The example below should return the UserId's 15850 and 15858

    DbDataContext db = new DbDataContext();    
    int[] userIds = {15850, 15858};
    var users = db.tblUsers.Where(x => x.UserId.Equals(userIds));
    

    Possible or not?

    Thanks