ENTITY FRAMEWORK - make a query like SQL

Asked

Viewed 207 times

0

Fala Pessoal,

I have a problem that may be not doing correctly, I have a query in a table description field for example

Select description table Where field like 'test%Description'

How do I do this search with Entity framework? because if I use the term o Contains, he would do +/- as like '%teste descricao%', but that’s not what I want.

  Context.Item.Where(c=> c.descricao.Contains(termo)).toList();

Thanks for the Help

Alex

2 answers

1

I have no way to test it here, but I believe it can stay that way:

Whereas you want the syntax for the same result of like 'teste%descricao':

Context.Item.Where(c=> c.descricao.StartsWith("teste") && c.descricao.EndsWith("descricao")).toList();
  • The problem is if I have a larger term for example TV Led 40 , I think I’ll have to use the Entity framework sqlQuery....

  • I didn’t understand the question of the greater term...rs

  • Sorry, a term greater than 2 words 3 four etc...each term would have to have a joker '%'

  • there’s a problem with what you’re wanting, because like 'teste%descricao' is different from like 'teste%' or like '%descricao' What’s your real need? @Alexbecker

  • Ola Rovann, I have a search field that you should search by the terms reported on the base in this way : e.g. " test Description of anything" like 'test%Description%of anything' wanted a way to do it by the Entity framework but it seems a bit complex, the best way to use it I think is to do the search with sqlQuery.... I don’t see any other way....

  • that’s exactly what: 'teste%descricao%de%qualquer%coisa' that I put in the comment above. that doesn’t make much sense, that’s exactly what you want ?

Show 1 more comment

0

You can use Startwith, which would be like Like 'Test%' or Endwith, which would be like Like '%Test'. Following example:

Context.Item.Where(c=> c.descricao.StartsWith(termo)).toList();

Context.Item.Where(c=> c.descricao.EndsWith(termo)).toList();
  • Face yes the problem is that the term may have more parameters, for example 'led 40 television'

Browser other questions tagged

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