0

I am relatively new to .net. I have a situation where I have a class

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; }
}

now my question is I want to add values to List.But facing some problem in adding values to attribute. I have tried the following,

 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();

How to add attribute values? Can anyone please help?

3
  • Not getting your actual problem. Please elaborate more. Commented Jan 5, 2018 at 6:24
  • In the above snippet, I can easily fill values for ean and sku . But as attributes is also a list I am not able to to so. Commented Jan 5, 2018 at 6:26
  • Did you try attributes = inventory.attributes in select new Product ? Commented Jan 5, 2018 at 6:31

3 Answers 3

1

You have multiple ways to write this

without lambda expression

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();

With 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();
Sign up to request clarification or add additional context in comments.

Comments

0

You didn't share about inventoryDetails but you could apply select for also 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();

Comments

0

When a model class cantains a List or collection fields we must intiate them at class constructor like this

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; }
}

If any case Attribues model is mapped to database then use Icollection instead of List

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.