Like search with LINQ using array

Asked

Viewed 165 times

2

Context:

I have the following form: inserir a descrição da imagem aqui 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:

inserir a descrição da imagem aqui

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));
   }

}
  • 1

    This "manufacturing code" is varchar, right?

  • Is this filter in the database or in memory? There is no way to infer this from the code shown.

  • 1

    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

  • But is the return already materialized in memory or is it a Queryable yet? It makes quite a difference.

  • is a Iqueryable.

  • In this case I think it will be complicated to assemble a logic that is translated to SQL without needing to build an expression.

  • 1

    I think you should read this: https://answall.com/a/361559/101 or is it even simpler.

  • 1

    @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

  • @That’s why I put the final paragraph in the answer there :)

  • x.it.cdProductFabricante is a String and if it is String it will contain the LIKE search pattern?

  • @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.

  • I erased the answer because she wasn’t suitable.

  • 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 =/

  • Augustovasques tranquil, @LINQ relaxes man, Manieiro’s link helped yes, I thank you for your help

Show 9 more comments

1 answer

0

Caique, researching about your problem I found that solution (in English)

In your case it would be applied in this way:

itens = itens.Where(x => codigosProdutoFabricante.Any(z => z.StartWith(x.it.cdProdutoFabricante)));

Recalling that, as this will be interpreted by the database, it will depend on whether this method has been implemented by preview.

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.