Field TextBox Search Enterprise and Category and Field DropDownList Cities Search Cities How to Make Includes in SQL - Asp.Net MVC

0

I developed a Code First system in Asp.Net MVC In this development I created a relationship very much and I want to do a research that includes these relationships in a research, I describe below better in code what I have developed

In addition to the Company and Category that will be searched by TextBox , I also want to search together with DropDownList Cities

My idea of Synthesis (Empresa || Categoria) && Cidade

I would also like that in the search of TextBox if the user type without emphasis the search returns the result, therefore I am having problems of returns in this case.

An example is I have Pharmacies the user types Farma or Pharmacy without accent does not return result.

I have the following classes. Company, Categories and the relationship class CompanyCategories.

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

NxN Relationship Class

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

No Controller I have a search that works, but it searches only Company, I also want to include 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());
}
    
asked by anonymous 25.10.2017 / 02:08

1 answer

1

Based on the premise of your previous question , simply 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());
}

In this way you are checking if the company has the city, along with the name or category.

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

    
25.10.2017 / 02:38