Home > Back-end >  Assign correct values of columns after grouping them
Assign correct values of columns after grouping them

Time:11-23

I have a list of 3 rows, comparing them with each other on certain condition to check if they match with any. If they match, need to aggregate the sum of inner list.

Below is the list I have. Need to check if the values of Property1, Property2 and Property4 (which is in InnerList) are same. If same, need to add the Percentage Column in inner list for the particular Property4 value and then display other corresponding properties as well.

{
List:[
{
Property1: "Apple",
Property2: "Fruit",
Property3: "USA",
Name: "aaa",
    InnerList:[{Property4 : 100,Percentage: 50.00}]
},
{
Property1: "Apple",
Property2: "Fruit",
Property3: "USA",
Name: "aaa",
    InnerList:[{Property4 : 100,Percentage: 50.00},
               {Property4 : 50,Percentage: 50.00}]
},
{
Property1: "Mango",
Property2: "Fruit",
Property3: "INDIA",
Name: "bbb",
    InnerList:[{Property4 : 75,Percentage: 50.00}]
}
]}

I use the below query to group them and aggregate them. Grouping and aggregation logic works perfectly, but can't get the correct values for other columns (Property3 and Name values)

Query used:

var result = list
    .GroupBy(x => new { x.Property1, x.Property2 })
    .Select(g => new
    {
        g.Key.Property1,
        g.Key.Property2,
        list[0].Property3,// not sure how to assign the correct value for Property3, Name for different groups
        list[0].Name, // It assigns only the List[0] value for all groups, but need its correct value for that group
        InnerList = g.SelectMany(x => x.InnerList)
            .GroupBy(x => x.Property4)
            .Select(g2 => new InnerList
            {
                Property4 = g2.Key,
                Percentage = g2.Sum(x => x.Percentage)
            })
    });

Expected

{
List:[
{
Property1: "Apple",
Property2: "Fruit",
Property3: "USA",
Name: "aaa",
    InnerList:[{Property4 : 100,Percentage: 100.00},
               {Property4 : 50,Percentage: 50.00}]
},
{
Property1: "Mango",
Property2: "Fruit",
Property3: "INDIA",
Name: "bbb",
    InnerList:[{Property4 : 75,Percentage: 50.00}]
}
]}

CodePudding user response:

It depends on what you expect to be there. The snippet below will take the first value. As I understand you wanted to achieve the same with list[0]

var result = list
    .GroupBy(x => new {x.Property1, x.Property2})
    .Select(g => new Type1()
    {
        Property1 = g.Key.Property1,
        Property2 = g.Key.Property2,
        Property3 = g.First().Property3, //instead of list[0]
        Name = g.First().Name, //this will set it to the value of the first element in the group
        InnerList = g.SelectMany(x => x.InnerList)
            .GroupBy(x => x.Property4)
            .SelectMany(g2 => new List<InnerType> ()
            {
                new() 
                {
                    Property4 = g2.Key,
                    Percentage = g2.Sum(x => x.Percentage)
                }
            }).ToList()
    }).ToList();

But you are not grouping your elements by Property3 and Name so that can be different. Imagine if the second object Property3 was "UK", what do you expect to see in the resulting object?

  • Related