0

How can I fill a list field using EF and linq of this model

public class Infraccion
{
    public int IdInfraccion { get; set; }
    public string Serie { get; set; }
    public int Numero { get; set; }
    public Direccion Direccion { get; set; }
    public string Comentario { get; set; }
    public DateTime Fecha { get; set; }
    public DateTime Hora { get; set; }
    public string Dominio { get; set; }
    public List<Contravencion> ListaDeContravenciones = new List<Contravencion>(); 
}

I DO know how to fill simple propertires, but dunno how to fill field List object where Contravencion is define like

public class Contravencion
{
    public string Articulo { get; set; }
    public string Inciso { get; set; }
    public int IdContravencion { get; set; }
    public string Descripcion { get; set; }
    public int UfijasMinimo { get; set; }
    public int UfijasMaximo { get; set; }
}

So far this is what I have

var listadoInfracciones = (from u in _context.Usuario
                           join ui in _context.UsuarioInfracciones on u.UsuarioId equals ui.UserId 
                           join i in _context.Infraccion on ui.InfraccionId equals i.InfraccionId
                           join d in _context.Direcciones on i.IdDireccion equals d.DireccionId
                           where ui.UserId == usuario.IdUsuario
                           select new Infraccion 
                           {
                               Comentario = i.Comentario,
                               Direccion = new Direccion
                               {
                                   Calle = d.Calle, 
                                   Entre1 = d.Interseccion1, 
                                   Entre2 = d.Interseccion2
                               }, 
                               Dominio = i.Dominio, 
                               Fecha = i.Fecha, 
                               Numero = i.Numero, 
                               Serie = i.Serie, 
                               ListaDeContravenciones = new List<Contravencion>()
                           }).ToList();

Where can't find the right way to fill the list of Contravenciones. Heres the DB model:

enter image description here

I've already seen these posts but do NOT fit my needs Easy way to fill object How to get data from the database into objectlists fast (with entity framework)

Community
  • 1
  • 1
Matias
  • 708
  • 10
  • 24
  • 2
    Have you tried `ListaDeContravenciones = i.ListaDeContravenciones`? – DavidG Oct 23 '15 at 00:52
  • 1
    have you seen these http://stackoverflow.com/questions/30466696/query-a-many-to-many-relationship-with-linq-entity-framework-codefirst http://stackoverflow.com/questions/5587288/many-to-many-query-in-entity-framework-4 – Daveo Oct 23 '15 at 01:03
  • I have now, but how do you get that list inside another list. I have to filter my "infracciones" table by user, and then get all "contravenciones" for each Infraccion. PS:Comment #1 did not solved my problem. – Matias Oct 26 '15 at 23:54

1 Answers1

0

Ok so i found a way to solve my problem. I'm pretty sure it's not the best way but i least have a result. If anyone can do this entirely with linq i certainly know would be helpful not just for me but for the entire comunity. Heres my code:

var listadoInfracciones = (from u in _context.Users
                       join ui in _context.User_Infraccion on u.UsuarioId equals ui.UserId 
                       join i in _context.Infraccions on ui.InfraccionId equals i.InfraccionId
                       join d in _context.Direccions on i.IdDireccion equals d.DireccionId
                       where ui.UserId == usuario.IdUsuario
                       select new Infraccion 
                       {
                           Comentario = i.Comentario,
                           Direccion = new Direccion
                           {
                               Calle = d.Calle, 
                               Entre1 = d.Interseccion1, 
                               Entre2 = d.Interseccion2
                           }, 
                           Dominio = i.Dominio, 
                           Fecha = i.Fecha, 
                           Numero = i.Numero, 
                           Serie = i.Serie,
                           IdInfraccion = i.InfraccionId
                       }).ToList();

        foreach (var inf in listadoInfracciones)
        {
            var test = (from contra in _context.Contravencions
                        where contra.Infraccion_Contravencion.Any(c => c.InfraccionId == inf.IdInfraccion)
                        select new Contravencion
                        {
                            Articulo = contra.Articulo,
                            Inciso = contra.Inciso,
                            UfijasMaximo = contra.UFijasMax,
                            UfijasMinimo = contra.UFijasMin,
                            Descripcion = contra.Descripcion,
                            IdContravencion = contra.ContravencionId
                        }).ToList();
            inf.ListaDeContravenciones = test;
        }

Cheers!

Matias
  • 708
  • 10
  • 24