List Foreach Loop C#

Haluk Karaca

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

Brett Caswell

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.

Calling Helper function


    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 Linq (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.

Combination of using Linq and passing a Helper Function to as delegate.

    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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Checking foreach loop list results C#

From Dev

foreach and traditional for-loop with List<T> in C#

From Dev

insert 2 List into tag <c:forEach> for loop Lists synchronously

From Dev

regarding C# adding objects to list using foreach loop

From Dev

Remove an item in a list while it's in a foreach loop c#

From Dev

Foreach loop a List<T> in C# with random data generation

From Dev

C# foreach set list property to loop index in one line using list.Foreach

From Dev

Refactor foreach loop to List<T>.ForEach(...)

From Dev

foreach loop List performance difference

From Dev

Trouble with creating a List in a foreach loop

From Dev

Converting list into agents in foreach loop

From Dev

Output list from ForEach Loop

From Dev

Populating a list within a list in C# not using foreach loop. better way?

From Dev

Removing an object from a list in a foreach loop of that list

From Dev

<c:foreach> to show list

From Dev

Usage of the foreach loop in C++

From Dev

c# threading and a foreach loop

From Dev

if statement in a foreach loop c#

From Dev

C# ForEach Loop (string declaration in loop)

From Dev

C# - Having a List<someInterface> and want to retrieve the different objects that use that interface not using foreach loop

From Dev

c# and Lists: How do I use the foreach loop to retrieve the objects form my list?

From Dev

First or Last element in a List<> in a foreach loop

From Dev

Updating single Column of List without Foreach Loop

From Dev

How to loop through Immutable List like forEach?

From Dev

How to use foreach loop on List type of class?

From Dev

Generic list Foreach loop with conditional statement

From Dev

php - adding totals for list of clients in foreach loop

From Dev

Filling List property of objects in a foreach-loop

From Dev

How to loop through list(perl) foreach?

Related Related

  1. 1

    Checking foreach loop list results C#

  2. 2

    foreach and traditional for-loop with List<T> in C#

  3. 3

    insert 2 List into tag <c:forEach> for loop Lists synchronously

  4. 4

    regarding C# adding objects to list using foreach loop

  5. 5

    Remove an item in a list while it's in a foreach loop c#

  6. 6

    Foreach loop a List<T> in C# with random data generation

  7. 7

    C# foreach set list property to loop index in one line using list.Foreach

  8. 8

    Refactor foreach loop to List<T>.ForEach(...)

  9. 9

    foreach loop List performance difference

  10. 10

    Trouble with creating a List in a foreach loop

  11. 11

    Converting list into agents in foreach loop

  12. 12

    Output list from ForEach Loop

  13. 13

    Populating a list within a list in C# not using foreach loop. better way?

  14. 14

    Removing an object from a list in a foreach loop of that list

  15. 15

    <c:foreach> to show list

  16. 16

    Usage of the foreach loop in C++

  17. 17

    c# threading and a foreach loop

  18. 18

    if statement in a foreach loop c#

  19. 19

    C# ForEach Loop (string declaration in loop)

  20. 20

    C# - Having a List<someInterface> and want to retrieve the different objects that use that interface not using foreach loop

  21. 21

    c# and Lists: How do I use the foreach loop to retrieve the objects form my list?

  22. 22

    First or Last element in a List<> in a foreach loop

  23. 23

    Updating single Column of List without Foreach Loop

  24. 24

    How to loop through Immutable List like forEach?

  25. 25

    How to use foreach loop on List type of class?

  26. 26

    Generic list Foreach loop with conditional statement

  27. 27

    php - adding totals for list of clients in foreach loop

  28. 28

    Filling List property of objects in a foreach-loop

  29. 29

    How to loop through list(perl) foreach?

HotTag

Archive