C # - How to make a lambda filter with more than one field?

5

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?

    
asked by anonymous 22.07.2015 / 16:47

2 answers

5

Create the function below and pass the values you want to filter as functions

    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;
    }
    
22.07.2015 / 17:04
3

2003 cars down with a value greater than 5000:

var lista = Fiat.Where(c => c.Ano <= 2003 & c.Valor > 5000);
    
22.07.2015 / 16:55