Passing values to the class in a loop

Anjali Agarwal 1,531 Reputation points
2024-10-16T00:47:35.4233333+00:00

I need to pass the JSON to a vendor and the vendor gives the GUID back, In order to achieve this, I declared following class:

public class PayParam
{
        public LineItemParam[] LineItems { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }
        public string CustomerId { get; set; }
        public string CompanyName { get; set; }
}
 public class LineItemParam
    {
        public string Sku { get; set; }
        public string Description { get; set; }
        public decimal UnitPrice { get; set; }
        public int Quantity { get; set; }
    }
I am constructing a JSON by passing values to the class like this:
 var parameters = new PayParam();
	  MailingInfo IdData = scData.FindData();
	  
	  string[] desc = {"Test1", "Test2", "Test3};
      int[] value={12.00. 13.00, 18.00}
	  parameters = new PayParam
      {
	     Phone = IdData.PhoneNumber,
         Email = IdData.Email.ToUpper(),
	     CustomerId = "1234",
		 CompanyName = "TEST NAME",
	  },
	    LineItems = new LineItemParam[]
          {
                  new LineItemParam
                          {
                              Sku = GetSKU(),
                              Description = "Test Desc",
                              UnitPrice =  12.00,
                              Quantity = 1
                          }
		   }
		   
	   The problem is, I need to pass multiple lineItems for each description so I need to do something like this:
```powershell
parameters = new PayParam
      {
	     Phone = IdData.PhoneNumber,
         Email = IdData.Email.ToUpper(),
	     CustomerId = "1234",
		 CompanyName = "TEST NAME",
	  },
	    foreach(var de in desc)
		   {
	           LineItems = new LineItemParam[]
               {
                  new LineItemParam
                          {
                              Sku = GetSKU(),
                              Description = de,
                              UnitPrice =  value,
                              Quantity = 1
                          }
		       }
		   }
	   The above code is not working. It only works if I put separate line item like this:

	   
```


```powershell
parameters = new PayParam
      {
	     Phone = IdData.PhoneNumber,
         Email = IdData.Email.ToUpper(),
	     CustomerId = "1234",
		 CompanyName = "TEST NAME",
	  },
	    
	           LineItems = new LineItemParam[]
               {
                  new LineItemParam
                          {
                              Sku = GetSKU(),
                              Description = "Test1",
                              UnitPrice =  12.00,
                              Quantity = 1
                          },
				 new LineItemParam
                          {
                              Sku = GetSKU(),
                              Description = "Test2",
                              UnitPrice =  13.00,
                              Quantity = 1
                          }, 
						  
		       }
```

```sql
	 I want to pass these lineItems in a loop and not write them individually. If I write the lineItem individually, my code works, but if I pass them in a loop then it does not work.
```
Developer technologies ASP.NET Other
Developer technologies C#
{count} votes

Accepted answer
  1. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2024-10-16T08:30:16.7066667+00:00

    Hi @Anjali Agarwal,

    I noticed that your problem has been solved on other platforms.

    I have another method here, I hope it will be useful to you. Use List<LineItemParam>.

    var parameters = new PayParam();
    List<LineItemParam> items = new List<LineItemParam>();
    MailingInfo IdData = scData.FindData();
    string[] desc = { "Test1", "Test2", "Test3" };
    double[] value = { 12.00, 13.00, 18.00 };
    for (int i = 0; i < desc.Length; i++)
    {
        items.Add(new LineItemParam
        {
            Sku = GetSKU(),
            Description = desc[i],
            UnitPrice = 12.00,
            Quantity = 1
        });
    }
    parameters = new PayParam
    {
        Phone = IdData.PhoneNumber,
        Email = IdData.Email.ToUpper(),
        CustomerId = "1234",
        CompanyName = "TEST NAME",
        LineItems = items,
    };
    
     public class PayParam
     {
         public List<LineItemParam> LineItems { get; set; }
         public string Phone { get; set; }
         public string Email { get; set; }
         public string CustomerId { get; set; }
         public string CompanyName { get; set; }
     }
     public class LineItemParam
     {
         public string Sku { get; set; }
         public string Description { get; set; }
         public double UnitPrice { get; set; }
         public int Quantity { get; set; }
     }
    

    User's image

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2024-10-16T16:21:11.48+00:00

    you just use linq:

    var parameters = new PayParam();
    var IdData = scData.FindData();
    var lineItems = new[] {("Test1",12.00m) , ("Test2",13.00m), ("Test3", 18.00m) };
    var parameters = new PayParam
          {
    	     Phone = IdData.PhoneNumber,
             Email = IdData.Email.ToUpper(),
    	     CustomerId = "1234",
    		 CompanyName = "TEST NAME",
    	  },
    	  LineItems = lineItems.Select(d => new LineItemParam
          {
             Sku = GetSKU(),
             Description = d.Item1,
             UnitPrice =  d.Item2, 
             Quantity = 1
          }).ToArray();
    

    if you need to use two arrays its:

          LineItems = desc.Select((d,i) => new LineItemParam
          {
             Sku = GetSKU(),
             Description = d,
             UnitPrice = values[i], 
             Quantity = 1
          }).ToArray();
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.