0
I developed a Code First Asp.Net MVC
In this development I created a relationship very much and I want to do a research that includes this relationships in a research, below I describe better in code what I developed
Besides the Company and Category that will be researched by TextBox
I also want to research along with the DropDownList
Cities
My idea of Synthesis (Empresa || Categoria) && Cidade
I would also like that in the research of TextBox
if the user type without accent the search returns the result, because I am having problems of returns in this case.
An example is I have registered Pharmacies the user types Farma or Pharmacy no accent returns no result.
I have the following classes. Company, Categories and Relationship Class.
Business Class
public class Empresa
{
public int Id { get; set; }
public string Nome { get; set; }
public string Telefone { get; set; }
public string Cidade {get; set;}
public ICollection<EmpresaCategoria> CategoriaEmpresa { get; set; }
}
Class Category
public class Categoria
{
public int Id { get; set; }
public string Nome { get; set; }
public ICollection<EmpresaCategoria> CategoriaEmpresa { get; set; }
}
Relationship Class Nxn
public class EmpresaCategoria
{
[Key]
public int Id { get; set; }
[ForeignKey("Categoria")]
public int CategoriaId { get; set; }
[ForeignKey("Empresa")]
public int EmpresaId { get; set; }
public virtual Categoria Categoria { get; set; }
public virtual Empresa Empresa { get; set; }
}
In the Controller
I have a research that works, but it researches only Company, I also want to include the Category
public ActionResult Resultado(string empresa, string cidade)
{
if (string.IsNullOrEmpty(pesquisaEmpresaOuCategoria))
{
return Redirect("Index");
}
/*
*ADICIONEI UM COMBO BOX COM A CIDADE
*Quero Incluir a categoria nesta pesquisa
*include
*/
//AQUI PREENCHE O COMBO BOX COM CIDADES
ViewBag.cidade = new SelectList(db.Empresa.Select(a => a.Cidade).Distinct());
return View(db.Empresa.Where(x => x.Nome.Contains(empresa) && x.Cidade.Contains(cidade)).ToList());
}
i took this your code and giving the following error, "bool" does not contain a definition for Tolist()
– Cyberlacs
@cyberlacs I put the parentheses in the wrong place. I edited the answer, test now
– Randrade
Perfect worked but was still missing a parentheses and so I solved the problem with your solution. Return View(db.Empresa.Where(x => x.Cidade.Contains(city) && (x.nome.Contains(company) || x.CategoryEmpresa.Any(c => c.Categoria.Name.Contains(company))) . Tolist());
– Cyberlacs
@cyberlacs You are correct. It is that you are returning in
View(db.Empresa...)
. In my tests I created a list, and did not have this last parenthesis. Thanks for the comment. P.S.: I edited the answer as your suggestion– Randrade
I’m having trouble with this instruction regarding the return to Topagedlist() I asked the question at this url you could see the error: https://answall.com/posts/comments/522666?noredirect=1
– Cyberlacs