简体   繁体   中英

Using linq for multiple condition in where

I have a list of conditions and I want to do something like this using linq in C#:

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

Thank you

In your case, converting where statement as below is enough:

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

Use IEnumerable<T>.Contains(T) method:

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

or

var filtred_list = from o in oldList
                   where conditions.Contains(o.attribut)
                   select o;
var filtred_list = oldList.Where(x => conditions.Any(c => c.attribut == x));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM