LINQのWhere IN句 [重複] 質問する

LINQのWhere IN句 [重複] 質問する

SQL Server の where in 句と同様の where in 句を作成するにはどうすればよいですか?

自分で作ったのですが、誰かこれを改良していただけませんか?

    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;
    }

ベストアンサー1

この式は、あなたが達成したいことを実現するはずです。

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

おすすめ記事