简体   繁体   English

从另一个类C#获取属性

[英]Get property from another class c#

I´m trying to make a shop in C#, which is composed in this moments these tables: 我试图在C#中开一家商店,这些表格在此时组成:

-products(id, name, desc, Images) -images(id, name, path, product_id) -产品(ID,名称,描述,图像)-图像(ID,名称,路径,产品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: 我试图使用此系统将所有数据存储在shop.aspx.cs中:

   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 . 假设,你需要显示可用的第一个图像Images集合中的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! 感谢您的帮助!

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

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