2
I would like to know how best to prepare a query that searches in all fields of a table.
Let’s say I own a news site, and on this site I have a input
for Search
in my template. When entering the information in this template, it needs to search in several columns in the table, such as title, description, author, etc. What is the best way to do this?
One way to do this search is by using IFs
, but let’s face it that is not good to use. And from this premise comes my doubt.
An example would be:
public ActionResult Buscar(string texto)
{
var titulo= AppService.ObterPorTitulo(texto);
if (!titulo.Any())
{
var descricao= AppService.ObterPorDescricao(texto);
if (!titulo.Any())
{
var autor= AppService.ObterPorAutor(texto);
return View("index", autor);
}
return View("index", descricao);
}
return View("Index", titulo);
}
I think that would be a gambiarra. Then comes my doubt: How to search in all fields in a single Action
I don’t have a context for this, but I had this doubt, so I posted only this example. But you were able to clarify my doubt. Thank you!
– Randrade
Another approach would be to filter on the client side thinking of the screen already loaded with all data, using the Angular.js Filter: https://docs.angularjs.org/api/ng/filter/filter
– Julio Borges
@Julioborges This approach is not good especially if the volume of news is too large. It would be necessary to load the whole table on screen and then filter.
– Leonel Sanches da Silva
@Gypsy omorrisonmendez, really, thinking that way is not ideal. Vlw.
– Julio Borges