The Code I Generated
TrendyolProductAddlist model = new TrendyolProductAddlist
{
items = new List<ProductAdditems>
{
new ProductAdditems
{
barcode=entegrasyon_barkod,
title=urun_baslik,
productMainId=stok_kodu,
brandId=trendyol_marka_id,
categoryId=trendyol_kategori_id,
quantity=stok,
stockCode=stok_kodu,
dimensionalWeight=desi,
description=urun_aciklama,
currencyType=para_birimi,
listPrice=Convert.ToDecimal(trendyol_psf),
salePrice=Convert.ToDecimal(trendyol_price),
vatRate = urun_kdv,
cargoCompanyId=kargo_id,
images = new List<Productimagelist>
{
new Productimagelist { url = resim1 }
} ,
attributes = new List<Productattributeslist>
{
new Productattributeslist { attributeId=renk_id, customAttributeValue= renk },
new Productattributeslist { attributeId=cinsiyet_id, attributeValueId= cinsiyet_valueid },
new Productattributeslist { attributeId = yasgrubu_id, attributeValueId = yasgrubu_valueid }
}
}
}
};
Class :
public string attributeId { get; set; }
public string attributeValueId { get; set; }
public string customAttributeValue { get; set; }
How can I get the code like this? I want to add a new list as many rows in dgw
attributes = new List<Productattributeslist>
{
foreach (DataGridViewRow dgwRow in dataGridView1.Row)
{
new Productattributeslist { attributeId = dgwRow.Cells[0].Value , customAttributeValue = dgwRow.Cells[1].Value },
}
}
how many lines in the dgw i want to use sending a attributess will happen, but it will not be possible to loop it are visible
Your concern and issue seems to be support for Looping Control Flow as it relates to Object and Collection Initializers; The syntax doesn't seem to support Looping inline (that would then expand into the object initializer), but you can achieve the result by invocations during right-hand assignment.
In the snippet you provided, your objective is to generate an initialized List<Productattributeslist>
instance based on the collection of DataGridViewRow
in dataGridView1.Row
.
attributes = GenerateProductAttributesList(dataGridView1.Rows)
#region "Helper Functions" // Can be defined as private static, or local .. depends on scope and testability you have in mind
List<Productattributeslist> GenerateProductAttributesList(DataGridViewRowCollection dgwRows) {
var resultList = new List<Productattributeslist>();
foreach (DataGridViewRow dgwRow in dgwRows)
{
resultList.Add(SelectProductAttributesListItem(dgwRow));
}
return resultList;
}
Productattributeslist SelectProductAttributesListItem(DataGridViewRow dgvwRow) => new Productattributeslist()
{
attributeId = dgvwRow.Cells[0].Value,
customAttributeValue = dgvwRow.Cells[1].Value
};
#endregion
using System.Linq;
at top) attributes = dataGridView1.Rows.Cast<DataGridViewRow>().Select((dgvwRow) => new Productattributeslist()
{
attributeId = dgvwRow.Cells[0].Value,
customAttributeValue = dgvwRow.Cells[1].Value
}).ToList()
The above Linq sample uses an anonymous delegate that's defined and passed to .Select
method. It's usually the way to go for simple mappings, but you might consider declaring a method to pass.
attributes = dataGridView1.Rows.Cast<DataGridViewRow>().Select(SelectProductAttributesListItem).ToList()
#region "Helper Function"
Productattributeslist SelectProductAttributesListItem(DataGridViewRow dgvwRow) => new Productattributeslist()
{
attributeId = dgvwRow.Cells[0].Value,
customAttributeValue = dgvwRow.Cells[1].Value
};
#endregion
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments