filter list of registered assets by room

Asked

Viewed 48 times

2

I’m doing a heritage system, where I can register a heritage and link to it a room. In this system I have a tab of reports, where I have a field that I must type the name of a room and a button to filter. When I click on the button I should return all the patrimonies registered with that room, but it returns me all the patrimonies.

I have the Reporiocontroller class with the search method

public IActionResult Busca(string nome)
{
  var resultado = _relatorioService.listarPatrimonios(nome);
  return View(resultado);
}

And I have the Reportarioservice class with the method

public List<Patrimonio> listarPatrimonios(string local)
{
  var resultado = from obj in _context.Patrimonio select obj;
  return resultado.Include(obj => obj.Local).Include(obj => obj.Equipamento).OrderBy(obj => obj.Id).ToList();
}

Could someone tell me where the mistake is?

local model

namespace CheckPatWebMVC.Models
{
  public class Local
  {
    public int Id { get; set; }
    public string Nome { get; set; }
  }
}

heritage model

namespace CheckPatWebMVC.Models
{
  public class Patrimonio
  {
    public int Id { get; set; }
    public int NumeroPatrimonio { get; set; }
    public string NumeroSerie { get; set; }
    public Equipamento Equipamento { get; set; }
    public int EquipamentoId { get; set; } //Garantir que o Id deverá existir
    public Local Local { get; set; }
    public int LocalId { get; set; } //Garantir que o Id deverá existir
    public string Coordenadas { get; set; }
    public string Usuario { get; set; }
    public string Observacao { get; set; }
    public bool Manutencao { get; set; }
  }
}
  • 2

    Could you share the models of the classes? From what I saw there in your code in no time in the listPatrimonios you end up filtering by site. You can use a LINQ Where to filter in the heritage context

  • Thanks for the indication. Do you mind pointing out what was different from the answer, so I can correct it? Obg.

1 answer

0


How do you have a table/template for Local, the ideal would be to use a combobox (or autocomplete) to list existing options and select the location. This way, instead of passing the name, would pass the id of the site.

If you prefer to go as is, passing the name of the place, you can do something like:

public List<Patrimonio> listarPatrimonios(string local)
{
  return _context.Patrimonio
                 .Include(obj => obj.Local) // faz join com local
                 .Include(obj => obj.Equipamento) // faz join com equipamento
                 .Where(patrimonio => patrimonio.Local.Nome == local) // filtra pelo local informado
                 .OrderBy(obj => obj.Id) // ordena pelo id do patrimonio
                 .ToList();
}

  • It still didn’t work, it must be because he picked up by Localid, but the localid only guards Id, I saw in a course that do this so that the FK is not null. From what I’ve been reading, I would now have to access by the Patrimonioformviewmodel class that owns the public Patrimonio Patrimonio { get; Sep; }, public Icollection<Local> Locals { get; Sep; and public Icollection<Equipment> Equipment { get; Sep; } } but I don’t know if my logic is correct.

  • 1

    @Felipesperanza I just created a minimal example and the code above works. https://github.com/tvdias/StackOverflowPT/blob/SOpt-472620/WebMvc/SampleMvc/Controllers/PatrimoniosController.cs#L24

  • The problem with the question code is the lack of .Where. As for FK, the Entity framework takes care of it itself, if your DbContext not in trouble.

  • Solved! Thank you all for your help. It worked through the method provided on Github

Browser other questions tagged

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