2
Context:
I have the following form: On this screen the user can filter materials by some of their properties. The filter highlighted in the image allows users to search for one or more manufacturing codes, and for more than one code it is necessary to separate the values with ;.
In addition there is a button on the side that indicates if the search for these values should return only what is exactly the same or what contains those codes.
Example:
Imagine the following data and filters:
Completed "1234;111" and button exact is selected, therefore the expected output is only the material bbbb which had code 111.
Completed "1234;111" and button contains is selected, therefore, the expected output is only the materials aaaa and bbbb.
The question is how to make this second filter with LINQ, considering the code below:
var itens =
from it in db.TABELA
join f in db.TABELA2
on it.id equals f.id
where it.Ativo == true
select new { it, f};
if (!string.IsNullOrEmpty(filtro.CdProdutoFabricante))
{
var codigosProdutoFabricante = filtro.CdProdutoFabricante.Replace(" ", "").Split(';');
if (filtro.TipoBuscaCdProdutoFabricante == 0)
{
//Busca por like
}
else //Busca por valor exato
{
itens = itens.Where(x => codigosProdutoFabricante.Contains(x.it.cdProdutoFabricante));
}
}
This "manufacturing code" is varchar, right?
– Jéf Bueno
Is this filter in the database or in memory? There is no way to infer this from the code shown.
– Jéf Bueno
Varchar. The variable items is the return of a query Linq to entities, being that in the expression lambda o x it. it represents the material table
– Caique Romero
But is the return already materialized in memory or is it a Queryable yet? It makes quite a difference.
– Jéf Bueno
is a Iqueryable.
– Caique Romero
In this case I think it will be complicated to assemble a logic that is translated to SQL without needing to build an expression.
– Jéf Bueno
I think you should read this: https://answall.com/a/361559/101 or is it even simpler.
– Maniero
@Maniero I don’t know if I missed something, but it seems to me that the query AP needs would be a 'concatenation' of OR. There it is not as simple as making several calls to the Where method
– Jéf Bueno
@That’s why I put the final paragraph in the answer there :)
– Maniero
x.it.cdProductFabricante is a String and if it is String it will contain the LIKE search pattern?
– Augusto Vasques
@LINQ Yes, it’s exactly like you said a 'concatenation of conditions with OR'. The link that Manieiro posted in the other answer with Predicatebuilder meets the situation, I believe that the very answer posted here also serve not got to test it yet. It turned out that I will no longer implement this filter in the application because I find it little performatic and I managed to convince the applicant.
– Caique Romero
I erased the answer because she wasn’t suitable.
– Augusto Vasques
Like I said, @Caiqueromero. You’ll have to build the expression, maybe the lib of Maniero’s answer will help you. I wanted to do some tests and try to answer, but my day is not letting =/
– Jéf Bueno
Augustovasques tranquil, @LINQ relaxes man, Manieiro’s link helped yes, I thank you for your help
– Caique Romero