Many Relationship Query for Many Entity Framework

Asked

Viewed 561 times

4

I’m new to Entity Framework and Linq, so my doubt may be very simple, but it’s breaking my head.

I have two entities:

public class Fornecedor
    {
        public int FornecedorId { get; set; }
        public string Nome { get; set; }
        public virtual ICollection<Municipio> Municipios { get; set; }
    }


public class Municipio
    {
        public int MunicipioId { get; set; }
        public string Nome { get; set; }
        public virtual ICollection<Fornecedor> Fornecedores { get; set; }   
    }

I need to make an appointment to return all suppliers that serve a given municipality. I will pass as parameter the name of the municipality, e.g. são paulo, and I need to receive a listing with all suppliers that serve this municipality.

I hope I was clear.

Thank you, Alexandre Previatti

1 answer

5


In that case, it would be best to obtain the list of suppliers by the municipality, thus:

var municipios= db.Municipio.Where(m => m.Nome == nome).ToList();

If the query returns only one municipality, or you want to return only one municipality, you can use the .FirstOrDefault(), in this way:

 var municipio= db.Municipio.FirstOrDefault(m => m.Nome == nome);

To access suppliers just access the list of suppliers of the municipality.

municipio.Fonecedores

You can use the .Select() to get only the list too, this way:

var fornecedores= db.Municipio.FirstOrDefault(m => m.Nome == nome).Select(m => m.Fornecedores);

Remembering that db is your database context.

Editing

As remembered by @Jamestk in the comments, it is worth mentioning some points:

  • If the Lazy Loading is disabled, you must add the .Include() in your code. ex: var municipio= db.Municipio.Include(m => m.Fornecedores).FirstOrDefault(m => m.Nome == nome);
  • You could also search for the employees, would look something like this: db.Funcionarios.Where(f => f.Municipios.Any(m => m.Nome == "Sao Paulo"))
  • 1

    I would just like to remind you that if Lazy Loading is disabled it will be necessary to do Vendor Include.

  • That despite f => f remember to Fornecedor, is not. In this case it makes direct reference to Municipio and that then it could be anything, ckmo x => x. In his example the query is through the Name of the Municipality.

  • Last comment. It could be through Funcionarios also: db.Funcionarios.Where(x => x.Municipios.Any(m => m.Id == 1)).

  • @Jamestk I made the edits based on the comments. Thank you for remembering.

  • @Randrade Thanks for the reply, it was very helpful. If I had one more entity, for example. Servico, where would you like to find the suppliers that provide a certain type of service in a given city? in this case, would have two many relationships for many.

  • @Alexandrepreviatti You can put more than one parameter inside the .Where(), for example: .Where(x => x.Nome == 'Sao Paulo' && x.Servico.Nome == 'Servico1'). Something like that, depending on your relationship

Show 1 more comment

Browser other questions tagged

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