Reputation: 21
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?
Upvotes: 0
Views: 82
Reputation: 44
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>();
}
Upvotes: 0
Reputation: 1782
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();
Upvotes: 1
Reputation: 13146
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();
Upvotes: 0