如何解决列出 Foreach 循环 C# 调用辅助函数使用 Linqusing System.Linq; 位于顶部结合使用 Linq 并将辅助函数传递给作为委托
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 }
}
}
}
};
类:
public string attributeId { get; set; }
public string attributeValueId { get; set; }
public string customAttributeValue { get; set; }
我怎样才能得到这样的代码? 我想在 dgw 中添加一个与行一样多的新列表
attributes = new List<Productattributeslist>
{
foreach (DataGridViewRow dgwRow in dataGridView1.Row)
{
new Productattributeslist { attributeId = dgwRow.Cells[0].Value,customAttributeValue = dgwRow.Cells[1].Value },}
}
我想在 dgw 中使用多少行发送属性会发生,但不可能循环它是可见的
解决方法
您的关注和问题似乎是对循环控制流的支持,因为它与 Object and Collection Initializers 相关;语法似乎不支持内联循环(然后会扩展到对象初始值设定项),但您可以通过在右侧赋值期间调用来获得结果。
在您提供的代码段中,您的目标是根据 List<Productattributeslist>
中的 DataGridViewRow
集合生成一个初始化的 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
使用 Linq(using System.Linq;
位于顶部)
attributes = dataGridView1.Rows.Cast<DataGridViewRow>().Select((dgvwRow) => new Productattributeslist()
{
attributeId = dgvwRow.Cells[0].Value,customAttributeValue = dgvwRow.Cells[1].Value
}).ToList()
上述 Linq 示例使用了一个匿名委托,该委托已定义并传递给 .Select
方法。这通常是进行简单映射的方式,但您可能会考虑声明一个要传递的方法。
结合使用 Linq 并将辅助函数传递给作为委托。
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。