How to include class in Asp.Net MVC search

Asked

Viewed 164 times

0

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

I have the following classes. Enterprise, Categories and the relationship class Business.

Business Class

public class Empresa
{
    public int Id { get; set; }

    public string Nome { get; set; }

    public string Telefone { 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 Controller I have a search that works, but it only searches Company, I also want to include the Category

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

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

        return View(db.Empresa.Where(x => x.Nome.Contains(pesquisaEmpresaOuCategoria)).ToList());
    }

1 answer

1


You can use the .Any() for that reason.

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

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

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

See working on . NET Fiddle.

Another way is for you to seek value in the relationship class, in this way:

return View(db.EmpresaCategoria.Where(x => x.Empresa.Nome.Contains(pesquisaEmpresaOuCategoria) || x.Categoria.Nome.Contains(pesquisaEmpresaOuCategoria)).ToList());

If you want to return only companies, you can use the .Select() to return only the company.

  • Randrade your answer worked, I would like you to answer me again, because I added new debts on this topic.

  • 1

    @cyberlacs As you have another question, the ideal is to open a new question with your new question. This way we keep track and can help more people. You can link this to the new one to be easier to understand. Taking advantage, if you really open a new question, explain a little more what you want, because I could not understand your editing properly. Any questions just let us know.

  • taking into account his statement I asked another question this link https://answall.com

Browser other questions tagged

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