Home > Blockchain >  Group informations by code (linq group by?)
Group informations by code (linq group by?)

Time:03-24

Environment : -angular for the web app -c# for my api -sql server for the data base

I have this informations from my data base :

public class Traductions
    {
        public int Id { get; set; }
        public int Lot { get; set; }
        public string Code { get; set; }
        public string Description { get; set; }
        public string IdLangage { get; set; }
        public string LibelleLangage { get; set; }
        public string Traduction { get; set; }
    }

If i make a get with the api that return me that format Json informations :

[
  {
  "Id": 1,
  "Lot" : 3,
  "Code" : "ABANDONDUTRAITEMENT",
  "Description" : "",
  "IdLangage": "FR",
  "LibelleLangage": "Francais",
  "Traduction": "Abandon du traitement"
  },
  {
  "Id": 2,
  "Lot" : 3,
  "Code" : "ABANDONDUTRAITEMENT",
  "Description" : "",
  "IdLangage": "EN",
  "LibelleLangage": "English",
  "Traduction": "Abandonment of       processing"
  }
  
]

But i want this type of Json format :

[
  {
  "Code": "ABANDONDUTRAITEMENT",
  "FR": "Abandon du traitement",
  "EN": "Abandonment of processing"
  },
]

I want to group the informations by Code to return all the Translations (traduction) by Code.

I think i have to do it in my api and not in my angular application

How can i do it ? With linq group by?

CodePudding user response:

somthing like this?

traductions.GroupBy(p => p.Code, 
                 (k, c) => new 
                         {
                             Code = k,
                             Data = c
                         }
                ).ToList();

https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.groupby?view=net-6.0

you can change the select to be how you want to represent the data in your angular application.

  • Related