You are falling into a matter of equality of different classes. To use a Contains
or a ==
, it is necessary that the two classes have some form of comparing.
To do this, create a method that returns bool
given a specific comparison between 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, it is possible to implement item-to-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 makes:
1) a=>
Receives in parameter with name a
the instance of the class A
that exists in the lista
.
2) listaComparacao.Any(b=>a.Igual(b))
Scroll through the comparison list, checking which are "equal" to the items in the initial list, executing the method of equality between the two classes.
A point to note: The method Igual
does not need to exist within the class A
. Just be visible in the context of the method you will filter.
EDIT
If you do not have the initial list, it is possible to filter on top of the 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 make a direct comparison between objects of the two classes by the operator ==
, making an Overload.
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);
}
}
Doing so, it is possible to 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();
Try and:
query = query.Where(x => testeDTOs.Contains(x))
– Francisco