简体   繁体   中英

Get property from another class c#

I´m trying to make a shop in C#, which is composed in this moments these tables:

-products(id, name, desc, Images) -images(id, name, path, product_id)

The most important class is the Product, whose fields are:

public class Product
{
    //Id of product
    public int Id { get; set; }
    //Ref of product
    public string Ref{ get; set; }
    //prize
    public double Prize{ get; set; }
    //Name
    public string NameProd{ get; set; }
    //Descr of product
    public string DescProduct { get; set; }
    //Iva of product
    public IVA Iva { get; set; }
    //Stock of the product
    public int Stock{ get; set; }
    //Category of product
    public Category Category { get; set; }
    //Images of the product
    public List<Imgs> Images { get; set; }
}

As you can see, the images are a list of the Class Image, composed by:

    //Id of image
    public int ID { get; set; }
    //Name of Img
    public string NameImg { get; set; }
    //Path of Img
    public string PathImg { get; set; }
    //FK product table
    [ForeignKey("Product")]
    public int Product_Id { get; set; }
    //Product
    public Product Product { get; set; }

The problem is when i´m trying to take the images of the corresponding product, who has a method in another class to take all the data from the database, the method is composed by:

      public IQueryable<T> GetAll() {
        return Context.Set<T>();
    }

And i´m trying to take all the data in a shop.aspx.cs, using this system:

   protected void Page_Load(object sender, EventArgs e)
    {
        context = new ApplicationDbContext();
        productManager = new ProductManager(context);

        var products = productManager.GetAll().Include(i => i.Images);

        foreach (var product in products)
        {
            var headRow = new TableHeaderRow();
            var row = new TableRow();

            headRow.Cells.Add(new TableCell { Text = product.NameProd});
            row.Cells.Add(new TableCell { Text = product.Category.ToString() });
            row.Cells.Add(new TableCell { Text = "<img src='"+ product.Images.Select(i => i.PathImg).ToList() +"'/>" });
            tbody.Controls.Add(headRow);
            tbody.Controls.Add(row);
        }
    }

But it doesn´t work, and i´m getting absolutely stucked. Any help is so helpful at this time... Thanks in advance!!

It's not clear which image do you need to show for the product as each product has multiple collection of images.

Assuming, you need to show the first image available in the Images collection in the Product .

You can replace

row.Cells.Add(new TableCell { Text = "<img src='"+ product.Images.Select(i => i.PathImg).ToList() +"'/>" });

to

row.Cells.Add(new TableCell { Text = "<img src='"+ product.Images.FirstOrDefault(i => i.PathImg) +"'/>" });

I think you are on the right track, but I can see an issue with your one to many relationship when you are accessing your images from the product. for example this line:

row.Cells.Add(new TableCell { Text = "<img src='"+ product.Images.Select(i => i.PathImg).ToList() +"'/>" });

I would change to:

var image = product.Images.FirstOrDefault();
if (image != null)
{
    row.Cells.Add(new TableCell { Text = "<img src='"+ image.PathImg +"'/>" });
}

Here I am using the first image found for your product, but you could easily change the logic to prefer one image over another.

Thanks to all the users that helped me, now is working! Only another question in general, in this case each product only has one image, but... if each product has more than one image, which is the best option to display the different images? Thanks for helping!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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