Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where IN clause in LINQ [duplicate]

Tags:

c#

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++)
        {
            _states.AddRange(
                 from states in _objdatasources.StateList()
                 where states.CountryCode == countrycode[i].ToString()
                 select new State
                 {
                    StateName  = states.StateName                    

                 });
        }
        return _states;
    }
like image 845
priyanka.sarkar Avatar asked Oct 10 '22 04:10

priyanka.sarkar


1 Answers

This expression should do what you want to achieve.

dataSource.StateList.Where(s => countryCodes.Contains(s.CountryCode))
like image 307
Daniel Brückner Avatar answered Oct 17 '22 16:10

Daniel Brückner