I Have this List of objects containing list of objects with tuple listProducts:
[
[
{
"group": "coloris",
"valeursAndUids": [
{
"Item1": "Beige",
"Item2": "QB32-20220325-486274"
},
{
"Item1": "Beige",
"Item2": "QB32-20220325-106045"
},
{
"Item1": "Venezia",
"Item2": "QB32-20220325-205994"
},
{
"Item1": "Venezia",
"Item2": "QB32-20220325-270903"
}
]
}
],
[
{
"group": "ref_commercial",
"valeursAndUids": [
{
"Item1": "29245",
"Item2": "QB32-20220325-486274"
},
{
"Item1": "29245",
"Item2": "QB32-20220325-106045"
},
{
"Item1": "29245",
"Item2": "QB32-20220325-205994"
},
{
"Item1": "29245",
"Item2": "QB32-20220325-270903"
}
]
}
]
]
I'm trying to regroup this list by values
What I have done:
List<Object> newList = new List<Object>();
foreach (var caracProd in listProducts)
{
var groupName = caracProd.group;
var groupValue = caracProd.valeursAndUids;
var groupedCustomerList = caracProd.valeursAndUids
.GroupBy(item => item.Item1)
.Select(group => new { groupName = group.Key, Values = group.Select(item => new { item.Item2 }).ToList() })
.OrderBy(item => item.groupName);
newList.Add(groupedCustomerList);
}
newList
[ [ { "groupName": "Beige", "Values": [ { "Item2": "QB32-20220325-486274" }, { "Item2": "QB32-20220325-106045" } ] }, { "groupName": "Venezia", "Values": [ { "Item2": "QB32-20220325-205994" }, { "Item2": "QB32-20220325-270903" } ] } ], [ { "groupName": "29245", "Values": [ { "Item2": "QB32-20220325-486274" }, { "Item2": "QB32-20220325-106045" }, { "Item2": "QB32-20220325-205994" }, { "Item2": "QB32-20220325-270903" } ] } ] ]
Desired result
[ [ { "Coloris": "Beige", "Values": [ { "Item2": "QB32-20220325-486274" }, { "Item2": "QB32-20220325-106045" } ] }, { "Coloris": "Venezia", "Values": [ { "Item2": "QB32-20220325-205994" }, { "Item2": "QB32-20220325-270903" } ] } ], [ { "ref_commercial": "29245", "Values": [ { "Item2": "QB32-20220325-486274" }, { "Item2": "QB32-20220325-106045" }, { "Item2": "QB32-20220325-205994" }, { "Item2": "QB32-20220325-270903" } ] } ] ]
Instead of groupName, I want to get the actual values but i'm having problem to getting the value of the groupName it's returning an anonymous problem.
I tried writing caracProd.group
unstead of groupName so I can have the value but got an error
Here's the error I encountered, is there a way to achieve what I want ?
Thank you, So much.
CodePudding user response:
You can't use anonymous types for grouping because in your case their properties' names are determined at runtime.
You could achieve a similar effect by using Dictionary
:
var newList = listProducts
.Select(prod => prod.valeursAndUids
.GroupBy(item => item.Item1)
.Select(g =>
{
var objAsDict = new Dictionary<string, object>
{
{ prod.group, g.Key },
{ "Values", g.Select(item => new { item.Item2 }).ToList() }
};
return objAsDict;
})
.OrderBy(item => item[prod.group]))
.ToList();
or the same with ExpandoObject
:
var newList = listProducts
.Select(prod => prod.valeursAndUids
.GroupBy(item => item.Item1)
.Select(g =>
{
var d = new ExpandoObject() as IDictionary<string, object>;
d.Add(prod.group, g.Key);
d.Add("Values", g.Select(item => new { item.Item2 }).ToList());
return d;
})
.OrderBy(item => item[prod.group]))
.ToList();