Sam
Sam

Reputation: 545

How do I append data to the list in loop?

I want to add AppGuid and AppName to httpResp list in the loop with same index as current index that loop is running on so that I can get httpResp list which includes AppGuid and AppName, how I do that?

    var data = new List<GetBuildTempClass>();
    List<BuildsClass.BuildsRootObject> httpResp = new List<BuildsClass.BuildsRootObject>();

    foreach (var app in data)
    {
        var request = new HttpRequestMessage(HttpMethod.Get, "apps/" + app.AppGuid + "/builds?per_page=200&order_by=updated_at");
        var response = await _client_SB.SendAsync(request);
        var json = await response.Content.ReadAsStringAsync();
        BuildsClass.BuildsRootObject model = JsonConvert.DeserializeObject<BuildsClass.BuildsRootObject>(json);

        if (model != null)
        {
            httpResp.Add(model);
        }
    }

    public partial class GetBuildTempClass
        {
            public Guid AppGuid { get; set; }
            public string AppName { get; set; }

        }

 public class BuildsClass
    {
        public class Data
        {
            public List<string> buildpacks { get; set; }
            public string stack { get; set; }
        }
        public class Lifecycle
        {
            public string type { get; set; }
            public Data data { get; set; }
        }

        public class App
        {
            public string href { get; set; }
        }

        public class Links
        {
            public App app { get; set; }
        }

        public class CreatedBy
        {
            public string guid { get; set; }
            public string name { get; set; }
            public string email { get; set; }
        }

        public class Resource
        {
            public string guid { get; set; }
            public DateTime created_at { get; set; }
            public DateTime updated_at { get; set; }
            public string state { get; set; }
            public object error { get; set; }
            public Lifecycle lifecycle { get; set; }
            public Links links { get; set; }
            public CreatedBy created_by { get; set; }
        }

        public class BuildsRootObject
        {
            public List<Resource> resources { get; set; }
        }
    }

Upvotes: 0

Views: 433

Answers (1)

Walter Verhoeven
Walter Verhoeven

Reputation: 4411

I guess you would like to have a list of tuple

var httpResp = new List<(BuildsClass.BuildsRootObject rootObj,Guid AppGuid,string appName)>();

To add you do

httpResp.Add( (model,Guid.Parse(resource.Guid),resource.AppName));

You can access then via the tuple in a loop or as individual named variables or in the list via the index.

var name= httpResp[0].appName;

if knows the name as we created a named tuple. if you do not name them you get item1, item2,... auto generated.

Upvotes: 1

Related Questions