Where to query using multiple properties of a list

0

I have the following scenario

public Teste[] GetTestes(List<TesteDTO> testeDTOs){
    IQueryble<Teste> query = GetAll<Teste>();
    //where ... 

}   


public class Teste()
{
    public int Prop1{ get; set;}
    public int Prop2 { get; set;}
    public int Prop3 { get; set;}

    //outras props
}

public class TesteDTO(){
    public int Prop1{ get; set;}
    public int Prop2 { get; set;}
    public int Prop3 { get; set;}
}

In the GetTestes method, I need to apply a where in the query, using all properties of TesteDTO . Virtually, ensure that the query will only return Testes "equal" to TestesDTO passed as parameter in method.

    
asked by anonymous 03.05.2018 / 20:57

1 answer

1

You are falling on a question of equality of different classes. To use Contains or == , it is necessary that the two classes have some way of comparing.

To do this, create a method that returns bool given a specific comparison of class properties.

public class A 
{
    public int Prop1 { get; set; }
    public int Prop2 { get; set; }
    public int Prop3 { get; set; }  
    public bool Igual(B b)
    {
        return  Prop1 == b.Prop1 && Prop2 == b.Prop2 && Prop3 == b.Prop3; //Aplique sua lógica de comparação entre as duas classes aqui
    }
}
public class B
{
    public int Prop1 { get; set; }
    public int Prop2 { get; set; }
    public int Prop3 { get; set; }
}

Having this method, you can implement item-by-item equality:

List<A> lista = new List<A>(); //Recebe a lista inicial
List<A> listaFinal = new List<A>(); //Filtra a lista com os parâmetros 
List<B> listaComparacao = new List<B>(); //Lista de filtro recebida por parâmetro
listaFinal = lista.Where(a=>listaComparacao.Any(b=>a.Igual(b))).ToList();

What the .Where last line does:

1) a=> Receives in the parameter named a the instance of class A that exists in lista .

2) listaComparacao.Any(b=>a.Igual(b)) Scrolls through the comparison list, checking which are equal to the items in the initial list, executing the equality method between the two classes.

One point to note: The Igual method does not have to exist within the A class. Just be visible in the context of the method you're going to filter.

EDIT

If you do not have the initial list, you can filter over IQueriable :

IQueryble<A> query = GetAll<A>();
List<A> listaFinal = new List<A>(); //Recebe a lista filtrada
List<B> listaComparacao = new List<B>(); //Lista de filtro recebida por parâmetro
listaFinal = query.Where(a=>listaComparacao.Any(b=>a.Igual(b))).ToList();

EDIT (2)

If you want, you can perform a direct comparison between objects of the two classes by the == operator, by overloading.

public class A 
{
    public int Prop1 { get; set; }
    public int Prop2 { get; set; }
    public int Prop3 { get; set; }  
    public bool Igual(B b)
    {
        return  Prop1 == b.Prop1 && Prop2 == b.Prop2 && Prop3 == b.Prop3; //Aplique sua lógica de comparação entre as duas classes aqui
    }
    public static bool operator ==(A a, B b)
    {
        return a.Igual(b);
    }

    public static bool operator !=(A a, B b)
    {
        return !a.Igual(b);
    }
}

In this way, you can use the query as follows:

IQueryble<A> query = GetAll<A>();
List<A> listaFinal = new List<A>(); //Recebe a lista filtrada
List<B> listaComparacao = new List<B>(); //Lista de filtro recebida por parâmetro
listaFinal = query.Where(a=>listaComparacao.Any(b=>a == b)).ToList();
    
06.05.2018 / 07:30