将数据添加到对象列表

时间:2018-01-05 06:20:33

标签: c# asp.net linq

我对.net比较新。我有一个班级的情况

public class Product
{
    public string sku { get; set; }
    public string ean { get; set; }
    public string price { get; set; }
    public string description { get; set; }
    public string long_description { get; set; }
    public string img_url { get; set; }
    public Size size { get; set; }
    public Style style { get; set; }
    public Brand brand { get; set; }
    public Color color { get; set; }
    public Category category { get; set; }
    public List<Attributes> attributes { get; set; }
}
public class Attributes
{
    public List<Attribute> attribute { get; set; }
}
public class Attribute
{
    public string name { get; set; }
    public string value { get; set; }
}

现在我的问题是我想向List.But添加值,在向属性添加值时遇到一些问题。 我试过以下,

 List<Product> products = new List<Product>();

                    products = (from inventory in inventoryDetails.AsEnumerable()
                                select new Product
                                {
                                    ean = !string.IsNullOrEmpty(inventory.ean) ? inventory.ean : null,
                                    sku = !string.IsNullOrEmpty(inventory.sku) ? inventory.sku : null,

                                }).ToList();

如何添加属性值?有人可以帮忙吗?

3 个答案:

答案 0 :(得分:0)

您没有分享inventoryDetails,但您也可以select申请attributes;

products = (from inventory in inventoryDetails.AsEnumerable()
    select new Product
    {
        ean = !string.IsNullOrEmpty(inventory.ean) ? inventory.ean : null,
        sku = !string.IsNullOrEmpty(inventory.sku) ? inventory.sku : null,
        attributes = inventory.attributes.Select(x => new Attributes
        {
            //Set properties
        })
    }).ToList();

答案 1 :(得分:0)

您有多种方法可以写这个

没有lambda表达

List<Product> products = (from inventory in inventoryDetails.AsEnumerable()
                        select new Product
                        {
                            ean = !string.IsNullOrEmpty(inventory.ean) ? inventory.ean : null,
                            sku = !string.IsNullOrEmpty(inventory.sku) ? inventory.sku : null,
                            attributes = inventory.attributes.Select(x => new Attributes
                            {
                                //Set properties

                            }).ToList()
                        }).ToList();

使用Lambda Expression

 List<Product> products = inventoryDetails.Select(inventory => new Product
        {
            ean = !string.IsNullOrEmpty(inventory.ean) ? inventory.ean : null,
            sku = !string.IsNullOrEmpty(inventory.sku) ? inventory.sku : null,
            attributes = inventory.attributes
            // if you want to set properties uncomment below lines and comment above line
            //attributes = inventory.attributes.Select(y => new Attributes
            //{
                ////Set properties

            //}).ToList()
        }).ToList();

答案 2 :(得分:0)

当一个模型类包含一个List或集合字段时,我们必须在这样的类构造函数中启用它们

public class Product
{
  public Product()  
  {
   this.attributes = new List<Attributes>();
  }
  public string sku { get; set; }
  public string ean { get; set; }
  public string price { get; set; }
  public string description { get; set; }
  public string long_description { get; set; }
  public string img_url { get; set; }
  public Size size { get; set; }
  public Style style { get; set; }
  public Brand brand { get; set; }
  public Color color { get; set; }
  public Category category { get; set; }
  public List<Attributes> attributes { get; set; }
}

如果 Attribues 模型被映射到数据库,则使用Icollection而不是List

public Product()  
{
  this.attributes = new ICollection<Attributes>();
}