【问题标题】:Include dynamic Entity Framework Core包括动态实体框架核心
【发布时间】:2020-05-24 11:10:41
【问题描述】:

我的数据库中有以下关系,product 有几个presentaciones_prouduct,并且在我的查询中,只有当它至少有一个产品展示时,我才需要包含它们,为此目的并创建了一个布尔属性product表中的lista_precios,作为指标,内部会处理。

进行以下查询,以包含列表,但需要很长时间,而且我有几个不需要它的产品:

var producto = await _context.Producto
                             .Select(x => new
                                          {
                                              x.Id,
                                              x.Nombre,
                                              x.NombreSecundario,
                                              x.MarcaId,
                                              x.IdCategoria,
                                              x.IdUnidad,
                                              x.Precio,
                                              x.PrecioCompra,
                                              x.Codigo,
                                              x.CantidadInicial,
                                              x.CantidadMinima,
                                              x.ListaPrecios,
                    Include------------>      x.PresentacionesProducto,
                                              x.Descripcion
                             }) 
                             .AsNoTracking()
                             .FirstOrDefaultAsync(x => x.Id== IdProducto);

现在我尝试在系统中造成最小的开销,只包括列表(如果有任何产品),这是动态的

 if (producto.ListaPrecios) {}

问题:在这种情况下,如何进行最有效的咨询?仅在您拥有产品演示的情况下才包含它们

【问题讨论】:

    标签: c# .net linq entity-framework-core


    【解决方案1】:

    怎么样:x.PresentacionesProducto.Where(p=>producto.ListaPrecios)

    使用您的代码:

                var producto = await _context.Producto
                             .Select(x => new
                                          {
                                              x.Id,
                                              x.Nombre,
                                              x.NombreSecundario,
                                              x.MarcaId,
                                              x.IdCategoria,
                                              x.IdUnidad,
                                              x.Precio,
                                              x.PrecioCompra,
                                              x.Codigo,
                                              x.CantidadInicial,
                                              x.CantidadMinima,
                                              x.ListaPrecios,
                                              PresentacionesProducto = x.PresentacionesProducto.Where(p=>producto.ListaPrecios),
                                              x.Descripcion
                             }) 
                             .AsNoTracking()
                             .FirstOrDefaultAsync(x => x.Id== IdProducto);
    

    【讨论】:

    • 这与我所寻找的非常接近,但是有没有办法在选择之外调用包含?因为搜索会这样延迟
    【解决方案2】:

    首先,您必须从 db 中获取该元素(如果存在),并在必要时加载导航属性。

    代码:

    var producto = await _context.Producto
                        .AsNoTracking() //Maybe it is not need
                        .FirstOrDefaultAsync(x => x.Id == IdProducto);
    
    if(producto != null && producto.ListaPrecios)
         _context.Entry<Producto>(producto)
              .Navigation(nameof(producto.PresentacionesProducto))
              .Load();                         
    

    【讨论】:

    • 请不要只发布代码作为答案,而是解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常质量更高,更有可能吸引投票。
    • 不要发表带有解释的评论,而是编辑您的答案以包含该解释。
    • 我认为我的问题并没有很好地向我解释,如果它没有真正的领域,那个查询不会给我带来任何东西,我需要的是始终带来产品并动态包含列出是否至少有一个
    • 好的,我明白你的问题了。我修改我的答案
    猜你喜欢
    • 2017-11-15
    • 1970-01-01
    • 2021-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-14
    相关资源
    最近更新 更多