Reputation: 1
i have the below data in the data table.
[
{'age': '25', 'name': 'mike'},
{'age': '25', 'name': 'john'},
{'age': '25', 'name': 'charles'},
{'age': '30', 'name': 'melanie'},
{'age': '30', 'name': 'cindy'}
]
I'd like to parse this array using Lodash or some not-so-messy way to get an array of objects where the Object has a key 'age' that maps to a string and a key 'names' that maps to an array of strings.
So the final resulting array would look like the following:
[
{'age': '25', 'names': ['john', 'mike', 'charles']},
{'age': '30', 'names': ['cindy', 'melanie']}
]
kindly suggest me a way forward in C# or VB.net to achieve this output.
Upvotes: 0
Views: 293
Reputation: 34429
Try following :
DataTable dt = new DataTable();
dt.Columns.Add("age", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Rows.Add(new object[] {25, "mike"});
dt.Rows.Add(new object[] {25, "john"});
dt.Rows.Add(new object[] {25, "charles"});
dt.Rows.Add(new object[] {30, "melanie"});
dt.Rows.Add(new object[] {30, "cindy"});
var groups = dt.AsEnumerable().GroupBy(x => x.Field<int>("age"));
foreach (var group in groups)
{
Console.WriteLine("age : '{0}', names : '{1}'", group.Key, string.Join(",", group.Select(x => x.Field<string>("name"))));
}
Console.ReadLine();
Upvotes: 1