按代码分组信息(linq group by?)

Group informations by code (linq group by?)

环境:-angular 用于 Web 应用程序 -c# 用于我的 api -sql 用于数据库服务器

我的数据库中有这些信息:

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; }
    }

如果我使用 api 获取 return 格式 Json 信息 :

[
  {
  "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"
  }
  
]

但我想要这种 Json 格式:

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

我想按代码将信息分组到 return 所有按代码的翻译(翻译)。

我想我必须在我的 api 中而不是在我的 angular 应用程序中这样做

我该怎么做?使用 linq 分组依据?

是这样的吗?

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

您可以将 select 更改为您希望在 angular 应用程序中表示数据的方式。