1
I wonder if it is possible to search two types of variables (one at a time) in a single textbox. Example:
public ActionResult Index(string pesquisa)
{
var usuario = from u in db.usuario
select u;
if (!String.IsNullOrEmpty(pesquisa))
{
usuario = usuario.Where(p => p.nomecompleto.Contains(pesquisa));
}
return View(usuario);
}
This my code above returns registered users of the site according to the name. I would like the site administrator to have the option to search users both by name and by CPF (which has been defined as int), simply typing the name or CPF.
Could someone explain if it is possible to do this? And if possible, I would like to take advantage of the code above.
There is Some difference between using Where or Firstordefault?
– Ryan Santos
Yes, the
FirstOrDefaultalready returns the first query element passed as a predicate, ornullif there are no results. TheWherereturns an iterable element that needs other methods to become the desired result. For your case,FirstOrDefaultis the most recommended.– Leonel Sanches da Silva