Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
Trying to get data from Shopify by running through loop.



public class OrderList
    {
        public List<Order> orders { get; set; }

    } 



public class Order 
    {
	public Order();

        public long? Id { get; set; }

        public Address BillingAddress { get; set; }

        public string BrowserIp { get; set; }

        public bool? BuyerAcceptsMarketing { get; set; }

        public DateTimeOffset? CancelledAt { get; set; }
    }  



 public List<Order> GetOrdersbyDateRange(DateTime fromdate,DateTime todate)
            {
                var totalorders = new List<Order>();
    
                long? lastid = 0;
    
                while (true)
                {
    
                    var json = _api.Get(string.Format("orders.json?created_at_min={0}&created_at_max={1}&since_id>{2}&limit=250", fromdate.ToString("yyyy-MM-dd"), todate.AddDays(1).ToString("yyyy-MM-dd"), lastid));
                    //var json = _api.Get(string.Format("orders.json"));
                    var searchorders = JsonConvert.DeserializeObject<OrderList>(json).orders;
                    if (searchorders.Count > 0)
                    {
                        lastid = searchorders[0].Id;
                        totalorders = totalorders.AddRange(searchorders);
                    }
                    else
                    {
                        break;
                    }
                   
                }
                return totalorders;
            }



Getting error near this line

totalorders = totalorders.AddRange(searchorders);

Error: Cannot implicitly convert type 'void' to System.Collections.Generic.List<ShopifySharp.Order>

I'm aware AddRange() does not return anything (void), hence the error.
I think m missing some simple point...but not able to fix it.

What I have tried:

Tried using 

<pre>totalorders = totalorders.Add(searchorders);


getting error

cannot convert from 'System.Collections.Generic.List<ShopifySharp.Order>' to 'ShopifySharp.Order'
Posted
Updated 1-Jun-21 4:11am

1 solution

The AddRange method adds the items to the specified list. It does not create and return a different list instance.
List<T>.AddRange(IEnumerable<T>) Method (System.Collections.Generic) | Microsoft Docs[^]

Just remove the totalorders = from your code:
C#
lastid = searchorders[0].Id;
totalorders.AddRange(searchorders);
 
Share this answer
 
Comments
Prathap Gangireddy 1-Jun-21 10:13am    
thank you..yes I understood that..trying to find a wordaround to fix this issue
Prathap Gangireddy 1-Jun-21 10:17am    
Silly me..sorry to bother..thank you..sometimes the simplest of mistakes take away half the day..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900