score:4

Accepted answer

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();

score:-1

first of all there is no connection between your collections, so namefiltered list does not know what to return.

i suggest you to make class

names
{

}

public string name{get;set;}
public bool fikter{get;set;}


var namefiltered = names.where(x=>x.fikter == true).toarray;

score:0

op wants to filter names using filter, which is made up of bool values. if you notice that they have the same number of elements, so if filter is true. it is required to be in the resulting set. there are a couple of ways you can achieve this. i would suggest using linq.zip, which combines two sequences.

ienumerable<bool> filter = new bool[] { true, true, false, true };
ienumerable<string> names = new string[] { "a", "b", "c", "d" };
var merged= filter.zip(names,(bo,str)=> new {bo, str});
var selected = merged.where(x=> x.bo).select(y=> y.str);

score:0

how about this

used where with index

var namefiltered = names.where((x, index) => filter.toarray()[index]);

or

var namefiltered= names.where((x, index) => filter.elementat(index));

score:1

not the best solution, but try this:

list<string> namefiltered = filter
    .select((x, i) => new { flag = x, index = i })
    .where(item => item.flag)
    .select(item => names.elementat(item.index))
    .tolist();
// output: a, b, d

score:3

var namefiltered = enumerable.range(0, names.count)
                             .where(n => filter[n])
                             .select(n => names[n])
                             .tolist();

score:3

there's zip method for union of corresponding pairs of two sequences:

filter.zip(names, (flag, name) => new { flag, name })
      .where(x => x.flag)
      .select(x => x.name)

zip makes this:

ienumerable<bool> filter = new[] { true, true, false, true };

ienumerable<string> names = new[] { "a", "b", "c", "d" };

filter.zip(names, (flag, name) => new { flag, name }) =
{
    { flag = true, name = "a" },
    { flag = true, name = "b" },
    { flag = false, name = "c" },
    { flag = true, name = "d" },
}

Related Query

More Query from same tag