Get daughters entities from the main table

0

Good I'm doing a test, and I have two tables Products and Brands. The relationship between them is 1: M that is, a brand can be present or can cover several products, while a product only belongs to one brand. This is the code that links to the product table.

var lista = db.Marcas.Include("Productos.Imagenes").ToList();

But from this list that will return all the marks, I want to obtain the products that are inside. Then I thought that in this way I was going to give them back, but apparently it does not return anything.

var pr = lista.Select(ma => ma.Productos.ToList());

If someone can explain to me how to obtain daughter entities from a parent class. And if there is a better way to do it. If possible I would like to make filters, and only get a certain product, I guess it can be with (Where), I've tried it but I really do not achieve anything. Thanks.

    
asked by Ronald Sanchez 09.05.2018 в 23:09
source

2 answers

1

To do what you want, just add the following to the Brand class:

    public class Marca
    {
        public int Id { get; set; }
        public string Nombre { get; set; }

        //esta es la relación de la que te hablaba Jesus Pocoata
        public virtual ICollection<Producto> Productos { get; set; } 
    }

and your Product class also add

public virtual Marca Marca { get; set; }

and in the Brand Index view you can do the following:

@model IEnumerable<WebApplication1.Models.Marca>

<table class="table table-hover">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Nombre)
        </th>
        <th>Productos</th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Nombre)
            </td>
            <td>
                //Aqui recorres todos los productos por cada Marca
                @foreach (var p in item.Productos.ToList())
                {
                    <p>@p.Nombre</p><br/>
                }
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
                @Html.ActionLink("Details", "Details", new { id = item.Id }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.Id })
            </td>
        </tr>
    }

</table>

I hope it helps you

    
answered by 10.05.2018 в 17:51
0

I do not recommend that you do this, since depending on the amount of data you will surely fall into a performance problem.

The best possible approach you can do is to create at least a service with two methods:

public class ServicioProductos
{
     public IList<Producto> ObtenerProductos()
     {}

     public IList<Imagen> ObtenerImagenesDeProductos(int[] productos)
     {}
}

These services will return the products and images of the products you send them.

After this, in the Controller you can call the two methods and cross by means of a model the data. Something like this:

Model

  public class ListadoProductosModel
{
    public IList<Producto> Productos{get;set;}
    ... demás propiedades
}

public class ProductoModel
{

  public IList<Imagen> Imagenes {get; set;}
   .... demás propiedades
}

Controller

public class ProductosController : Controller
{
   public IActionResult Get()
  {
    var productos = servicio.ObtenerProductos();
    var imagenes = servicio.ObtenerImagenes(productos.Select(c => c.Id).ToArray());

    var model = new ListadoProductosModel();
    model.Productos = productos; // hacer conversion

    foreach(var p in model.Productos)
       p.Imagenes = imagenes.Where(c=> c.ProductId == p.Id);

    return this.View(model);
  }
}

It's time to make more code, but your query will definitely be much more efficient.

    
answered by 11.05.2018 в 20:42