LINQ中的IN子句

时间:2009-06-06 14:08:05

标签: c# linq

如何使where in子句与SQL Server中的类似?

我自己做了一个,但有人可以改进吗?

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

8 个答案:

答案 0 :(得分:228)

这个表达式应该达到你想要达到的效果。

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

答案 1 :(得分:92)

这将转换为Linq to SQL中的where in子句......

var myInClause = new string[] {"One", "Two", "Three"};

var results = from x in MyTable
              where myInClause.Contains(x.SomeColumn)
              select x;
// OR
var results = MyTable.Where(x => myInClause.Contains(x.SomeColumn));

如果是您的查询,您可以执行以下操作......

var results = from states in _objectdatasource.StateList()
              where listofcountrycodes.Contains(states.CountryCode)
              select new State
              {
                  StateName = states.StateName
              };
// OR
var results = _objectdatasource.StateList()
                  .Where(s => listofcountrycodes.Contains(s.CountryCode))
                  .Select(s => new State { StateName = s.StateName});

答案 2 :(得分:31)

我喜欢它作为扩展方法:

public static bool In<T>(this T source, params T[] list)
{
    return list.Contains(source);
}

现在你打电话:

var states = _objdatasources.StateList().Where(s => s.In(countrycodes));

您也可以传递单个值:

var states = tooManyStates.Where(s => s.In("x", "y", "z"));

感觉更自然,更接近sql。

答案 3 :(得分:7)

public List<Requirement> listInquiryLogged()
{
    using (DataClassesDataContext dt = new DataClassesDataContext(System.Configuration.ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString))
    {
        var inq = new int[] {1683,1684,1685,1686,1687,1688,1688,1689,1690,1691,1692,1693};
        var result = from Q in dt.Requirements
                     where inq.Contains(Q.ID)
                     orderby Q.Description
                     select Q;

        return result.ToList<Requirement>();
    }
}

答案 4 :(得分:6)

“IN”子句通过.Contains()方法构建到linq中。

例如,要使所有人的.States为“NY”或“FL”:

using (DataContext dc = new DataContext("connectionstring"))
{
    List<string> states = new List<string>(){"NY", "FL"};
    List<Person> list = (from p in dc.GetTable<Person>() where states.Contains(p.State) select p).ToList();
}

答案 5 :(得分:4)

from state in _objedatasource.StateList()
where listofcountrycodes.Contains(state.CountryCode)
select state

答案 6 :(得分:2)

这个有点不同的想法。但它对你有用。我在linq主查询中使用了子查询。

<强>问题:

假设我们有文件表。架构如下 schema:文档(名称,版本,auther,modifieddate) 复合键:名称,版本

所以我们需要获得所有文档的最新版本。

<强> soloution

 var result = (from t in Context.document
                          where ((from tt in Context.document where t.Name == tt.Name
                                orderby tt.Version descending select new {Vesion=tt.Version}).FirstOrDefault()).Vesion.Contains(t.Version)
                          select t).ToList();

答案 7 :(得分:1)

public List<State> GetcountryCodeStates(List<string> countryCodes)
{
    List<State> states = new List<State>();
    states = (from a in _objdatasources.StateList.AsEnumerable()
    where countryCodes.Any(c => c.Contains(a.CountryCode))
    select a).ToList();
    return states;
}