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
I would just like to remind you that if Lazy Loading is disabled it will be necessary to do Vendor Include.
– JamesTK
That despite
f => f
remember toFornecedor
, is not. In this case it makes direct reference toMunicipio
and that then it could be anything, ckmox => x
. In his example the query is through the Name of the Municipality.– JamesTK
Last comment. It could be through
Funcionarios
also:db.Funcionarios.Where(x => x.Municipios.Any(m => m.Id == 1))
.– JamesTK
@Jamestk I made the edits based on the comments. Thank you for remembering.
– Randrade
@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.
– Alexandre Previatti
@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– Randrade