C# - How to make a lambda filter with more than one field?
My List:
public class Carro
{
public int Ano;
public double Valor;
}
List<Carro> Fiat = new List<Carro>();
Fiat.Add(new Carro {Ano = 2000, Valor = 5000 });
Fiat.Add(new Carro {Ano = 2000, Valor = 6000 });
Fiat.Add(new Carro {Ano = 2001, Valor = 7000 });
Fiat.Add(new Carro {Ano = 2002, Valor = 8000 });
How to make a filter in this list using lambda with more than one field?
5
Author: Benjamim Mendes Junior, 2015-07-22
2 answers
Create the function below and pass as function faces the values you want to filter
public List<Carro> Filtrar(List<Carro> lista, int Ano, double valor)
{
List<Carro> lstFiltrada = lista.Where(x => x.Ano == Ano && x.Valor == valor).ToList();
return lstFiltrada;
}
5
Author: Benjamim Mendes Junior, 2015-07-22 15:04:09
Cars from 2003 down with value greater than 5000:
var lista = Fiat.Where(c => c.Ano <= 2003 & c.Valor > 5000);
3
Author: FernandoNomellini, 2015-07-22 14:55:14