Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a grouped variable called groupValues
this groupValues has the following

var groupValues = allValues.Current.GroupBy(x => new { x.Id })
                   .Select(g => new
                   {
                      Id1 = g.Select(x => x.Id1),
                      Names = string.Join(", ", g.Select(x => 
                                x.Name).Distinct().ToList()),

                    allFields = g
});


now in the response
i want to retrieve this

"Values": [
        {
            "id": 2,
            "names": "name1, name2, name3",
            "totalPrice": "45",
             details:[
             {
              "name": "name1",
              "name_price": "25"
             }
             {
              "name": "name2",
              "name_price": "30"
             }
             {
              "name": "name3",
              "name_price": "null"
             }


        }
]


I'm having difficulty in retrieving the details in this format

What I have tried:

AllValues allValues = await service.getAllValues(request);


<pre> 
var groupValues = allValues.Current.GroupBy(x => new { x.Id })
                   .Select(g => new
                   {
                      Id1 = g.Select(x => x.Id1),
                      Names = string.Join(", ", g.Select(x => 
                                x.Name).Distinct().ToList()),

                    allFields = g
});


List<Value> valueList = new List<Value>();


foreach (var y in groupValues)
               {
                   var totalPrice = y.allFields.Select(x => x.Price).Sum();
                   valueList.Add(new Response(y.Names,totalPrice))

}


How to get the details part?

i made a class for details, but i don't know how to do it to appear in the response
Posted
Updated 25-Jun-21 3:11am
Comments
Richard Deeming 25-Jun-21 7:25am    
Not at all clear. We can't see your input data, and the grouped data doesn't match the required output structure.

Also, if you have two items with prices of 25 and 30, why would the total price be 45 instead of 55?

1 solution

This may help. I am using classes here that match your definition above. Change as needed:

C#
public class NameAndPrice {
   public string name { get; set; }
   public decimal name_price { get; set; }
}

public class ValueAndDetails {
   public int id { get; set; }
   public List<string> names { get; set; }
   public decimal totalPrice { get; set; }
   public List<NameAndPrice> details { get; set; }
}


Now fill an instance of that class like this:

C#
var result = allValues.Current
   .GroupBy(x => x.Id)
   .Select(g => new ValueAndDetails {
      id = g.Key,
      names = string.Join(", ", g.Select(x => x.Name)).Distinct().ToList(),
      totalPrice = g.Sum(x => x.Price),
      details = g.Select(x => new NameAndPrice {
         name = x.Name,
         name_price = x.Price
      }).ToList()
   };
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900