I cannot find out how to filter an array/list from another list array: I was looking for something like this:
IEnumerable<bool> Filter = new[] { true, true, false,true };
IEnumerable<string> Names = new[] { "a", "B", "c", "d" };
List<string> NameFiltered = Filter
.Where(c => c == true)
.Select(x => Names)
.ToList();
In general case (both Names and Filter are IEnumerable<T> only) I suggest Zip:
List<string> NameFiltered = Names
.Zip(Filter, (name, filter) => new {
name = name,
filter = filter, })
.Where(item => item.filter)
.Select(item => item.name)
.ToList();
If Filter is in fact an array (..Filter = new[]...) Where will do:
List<string> NameFiltered = Names
.Where((name, index) => Filter[index])
.ToList();
var NameFiltered = Enumerable.Range(0, Names.Count)
.Where(n => Filter[n])
.Select(n => Names[n])
.ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With