C# 包含动态实体框架核心

C# 包含动态实体框架核心,c#,.net,linq,entity-framework-core,C#,.net,Linq,Entity Framework Core,我的数据库中有以下关系,一个产品有几个presentaciones\u product,在我的查询中,我只需要在至少有一个产品表示的情况下包含它们,为此,我在产品表中创建了一个布尔属性lista\u precios,作为一个指示器,这将在内部处理 进行以下查询以包括列表,但这需要很长时间,我有几个产品不需要它: var producto = await _context.Producto .Select(x => new

我的数据库中有以下关系,一个产品有几个presentaciones\u product,在我的查询中,我只需要在至少有一个产品表示的情况下包含它们,为此,我在产品表中创建了一个布尔属性lista\u 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) {}

问题:在这种情况下,最有效的咨询方式是,仅当您有产品演示文稿时才包括它们。首先,您必须从dbif exist中获取该元素,如果需要,在加载导航属性后获取该元素

代码:


怎么样:x.PresentacionesProducto.Wherep=>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);

请不要只发布代码作为答案,而是解释代码的作用以及它如何解决问题。带解释的答案通常质量更高,更有可能吸引更多的选票。不要发表带解释的评论,但编辑你的答案以包含该解释。我认为我的问题对我解释得不好,如果没有真实字段,该查询不会给我带来任何东西,我需要的是始终带上产品,并动态地包括列表,如果它至少有一个好的,我理解你的问题。我修改了我的答案这与我想要的非常接近,但是有没有一种方法可以在select之外调用include?因为搜索就这样延迟了
            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);