简体   繁体   English

如何使用EF使用列表填充对象

[英]How to fill object with list using EF

How can I fill a list field using EF and linq of this model 如何使用该模型的EF和linq填充列表字段

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 我确实知道如何填充简单的属性,但不知道如何填充字段Contravencion定义的List对象,例如

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 . 在哪里找不到正确的方法来填写Contravenciones清单。 Heres the DB model: 这是数据库模型:

在此处输入图片说明

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) 我已经看过这些帖子,但是不适合我的需求轻松填充对象的方法 如何将数据从数据库快速获取 到对象 列表(使用实体框架)

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. 如果有人可以完全使用linq做到这一点,我当然知道这不仅对我有帮助,而且对整个社区都有帮助。 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! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM