Field Textbox search Company and Category and Field Dropdownlist Cities search Cities how to do Includes in SQL - Asp.Net MVC

Asked

Viewed 27 times

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

1 answer

1


According to the premise of his previous doubt, just do the following for your search:

public ActionResult Resultado(string pesquisaEmpresaOuCategoria, string cidade)
{
    if (string.IsNullOrEmpty(pesquisaEmpresaOuCategoria))
    {
        return Redirect("Index");
    }

    /*
     *Quero Incluir a categoria nesta pesquisa
     *include
     */

    return View(db.Empresa.Where(x => x.Cidade.Contains(cidade) && 
                                (x.Nome.Contains(pesquisaEmpresaOuCategoria) 
                                || x.CategoriaEmpresa.Any(c => c.Categoria.Nome.Contains(pesquisaEmpresaOuCategoria))))
                                .ToList());
}

This way you are checking if the company owns the city along with the name or category.

On the issue of accentuation, this question has the answers to your problem.

  • i took this your code and giving the following error, "bool" does not contain a definition for Tolist()

  • @cyberlacs I put the parentheses in the wrong place. I edited the answer, test now

  • 1

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

  • 1

    @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

  • 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

Browser other questions tagged

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