在其中使用linq的多个条件

时间:2018-04-27 10:40:33

标签: c# linq where

我有一个条件列表,我想在C#中使用linq做这样的事情:

var filtred_list = oldList.Where(
    foreach (int condition in conditions) {
        c => c.attribut == condition;
    }
);

谢谢

3 个答案:

答案 0 :(得分:2)

在您的情况下,转换where语句就足够了:

var filtred_list = oldList.Where(
        c => conditions.Contains(c.attribut);
    }
);

答案 1 :(得分:0)

使用IEnumerable<T>.Contains(T)方法:

var filtred_list = oldList.Where(c => conditions.Contains(c.attribut));

var filtred_list = from o in oldList
                   where conditions.Contains(o.attribut)
                   select o;

答案 2 :(得分:0)

var filtred_list = oldList.Where(x => conditions.Any(c => c.attribut == x));