Reputation: 889
I'm trying to filter a list with linq, the problem is that when filtering a list with linq it only returns the result of the filter, and im looking for a way to modify the list rather than getting a result of the filter, because im doing it on a foreach.
Here is the foreach im doing:
foreach(Filtro f in filtros)
{
if(f.Activo)
{
switch(f.TipoFiltro)
{
case TipoFiltro.nombre:
jugadoresFiltrados.Where(j => j.Nombre.Contains(f.ContenidoFiltro));
break;
case TipoFiltro.equipo:
jugadoresFiltrados.Where(j => j.EquipoJugador.Contains(f.ContenidoFiltro));
break;
case TipoFiltro.mundial:
break;
}
}
}
This won't work because I'm not modifiying the jugadoresFiltrados list.
Upvotes: 0
Views: 54
Reputation: 73253
Where returns the filtered list, it doesn't change the collection:
Returns an IEnumerable that contains elements from the input sequence that satisfy the condition.
So you need to assign the filtered list back to the collection, for example:
jugadoresFiltrados = jugadoresFiltrados.Where(j => j.Nombre.Contains(f.ContenidoFiltro));
If jugadoresFiltrados
is a List, you will need to call ToList() as well:
jugadoresFiltrados = jugadoresFiltrados.Where(j => j.Nombre.Contains(f.ContenidoFiltro)).ToList();
Upvotes: 2