C# – Where IN clause in LINQ


How to make a where in clause similar to one in SQL Server?

I made one by myself but can anyone please improve this?

    public List<State> Wherein(string listofcountrycodes)
        string[] countrycode = null;
        countrycode = listofcountrycodes.Split(',');
        List<State> statelist = new List<State>();

        for (int i = 0; i < countrycode.Length; i++)
                 from states in _objdatasources.StateList()
                 where states.CountryCode == countrycode[i].ToString()
                 select new State
                    StateName  = states.StateName                    

        return _states;

Best Answer

This expression should do what you want to achieve.

dataSource.StateList.Where(s => countryCodes.Contains(s.CountryCode))