Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, July 24, 2013 4:47 PM
I am writing to ask how best a List, based on a pre-defined class of Properties, can be populated in a foreach loop with C#;
Working with an ASP.Net Web API in a MVC application, to access an SQL Server database, query results are sent to a Windows 8 client application - via Json.Net; The second code sample below assigns the columns of Json object to string variables - the Json object has 23 rows;
The third code sample correctly assigns the values to the List - but only the last row is assigned, the List is then bound to a GridView in a Windows 8 client application; A class named Data.cs is included in the application, and contains public strings MetricDisplayName, LatamColor, etc.
Thanks in advance for insight, guidance;
var client = new HttpClient();
client.MaxResponseContentBufferSize = 1024 * 1024; // Read up to 1 MB of data
var response = await client.GetAsync(new Uri("http://localhost:42204/api/values"));
var result = await response.Content.ReadAsStringAsync();
dynamic jObj = JsonConvert.DeserializeObject(result);
foreach (var child in jObj.Children())
{
saveIDFM = (string)child[0];
saveIDFMTwo = (string)child[1];
saveArgentinaUruguayColor = (string)child[2];
saveBrazilColor = (string)child[3];
saveChileColor = (string)child[4];
saveColombiaColor = (string)child[5];
saveLatamNewMarketsColor = (string)child[6];
saveMexicoColor = (string)child[7];
savePeruColor = (string)child[8];
savePuertoRicoColor = (string)child[9];
saveVenezuelaColor = (string)child[10];
}
this.DataGrid.ItemsSource = new List<Data>
{
new Data { MetricDisplayName = saveIDFM, LatamColor = saveIDFMTwo, ArgentinaUruguayColor = saveArgentinaUruguayColor, BrazilColor = saveBrazilColor, ChileColor = saveChileColor, ColombiaColor = saveColombiaColor, LatamNewMarketsColor = saveLatamNewMarketsColor, MexicoColor = saveMexicoColor, PeruColor = savePeruColor, PuertoRicoColor = savePuertoRicoColor, VenezuelaColor = savePuertoRicoColor },
};
robert hellestrae
All replies (5)
Wednesday, July 24, 2013 4:51 PM ✅Answered | 1 vote
Is the Children method of the jObj class returning a collection or is it returning a string?
The best things in life are free, but the most valuable ones are costly...use opportunities well for there are others like you who deserves them, but don't have them...
Wednesday, July 24, 2013 7:31 PM ✅Answered | 1 vote
I'm not sure I fully understand your question but the following is one way to build a list of a class of properties:
var DataList = new List<Data>(); //List of class with properties.
begin foreach loop...
var itemValue = new Data() { new Data MetricDisplayName=saveIDFM, ......}; //class with the properties
DataList.Add(itemValue);
end foreach loop
Wednesday, July 24, 2013 10:46 PM ✅Answered | 1 vote
I realise that your question asked how to do this with foreach loop but if you want a more modern way, available for the last 5+ years (about 60 internet years) then you could write
var newListLatam = matrix.Select(img=>new Data{MetricDisplayName = img[0], LatamColor=img[1], etc});
Assuming the elements of matrix are strings
Paul Linton
Wednesday, July 24, 2013 5:08 PM
The Children method of jObj returns a collection of string values with no tokens; by indexing, I am able to access the columns of a given row; in debug mode, I am able to view the 23 rows - which contain string values separated by double quotes " "
robert hellestrae
Wednesday, July 24, 2013 7:53 PM
Thanks for the feedback; ended up with a similar solution;
List<Data> newListLatam = new List<Data>();
foreach (var image in matrix)
{
newListLatam.Add(new Data() { MetricDisplayName = image[0].ToString(), LatamColor = image[1].ToString(), ArgentinaUruguayColor = image[2].ToString(), BrazilColor = image[3].ToString() });
}
robert hellestrae