将 Javascript Reduce 函数转换为 C#
Convert Javascript Reduce Function to C#
我正在尝试将此函数从 Javascript 转换为 C#。
目标是接收合同列表并将它们分类为子组,如果它们具有相同的客户,则将它们捆绑在一起。
> let c1 = {id:1,name:"c1"}
> let c2 = {id:2,name:"c2"}
> let c3 = {id:2,name:"c3"}
> let c4 = {id:1,name:"c4"}
> let c5 = {id:3,name:"c5"}
> let list = [c1,c2,c3,c4,c5]
>
> const groupByKey = (array, property) => { return
> array.reduce((accumulator, object) => {
> const key = object[property];
>
> if (!accumulator[key]) {
> accumulator[key] = []
> }
> accumulator[key].push(object)
> return accumulator; }, {}) }
>
> groupByKey(list, "id")
>
> // input = let list = [c1,c2,c3,c4,c5] //output =
> {"1":[{"id":1,"name":"c1"},{"id":1,"name":"c4"}],"2":[{"id":2,"name":"c2"},{"id":2,"name":"c3"}],"3":[{"id":3,"name":"c5"}]}
// 最好输出是包含捆绑合同的列表列表。
更新:
我已经编写了函数,问题是我需要 return 只是合同 ID,而不是整个对象。
desired output [[c1.id,c4.id],[c2.id,c3.id],[c5.id]]
这是我目前拥有的。
public JsonResult checkForSameClient(long[] listOfContracts)
{
// here i grab the list of contracts ids then go to my repository and find //the whole objects to be compared.
IQueryable<ContratEspace> contrats = contratEspaceRepository.Get(1, listOfContracts.Count(), listOfContracts);
var contratList = contrats.ToList();
var finalArray = contrats.GroupBy(c => c.id_clientGestion).ToList();
return new JsonResult() { Data = finalArray };
}
但这不是 return 正确 Json.Data 我有错误:
SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at fromJson (angular.js:1282)
at defaultHttpResponseTransform (angular.js:10133)
at angular.js:10224
at forEach (angular.js:321)
at transformData (angular.js:10223)
at transformResponse (angular.js:10996)
at processQueue (angular.js:15552)
at angular.js:15568
at Scope.$eval (angular.js:16820)
由于 GroupBy 内置于 .NET 中,因此无需编写聚合函数。
如何在 C# 中对列表进行分组:
var list = new [] {
new { Id = 1, Name = "c1"},
new { Id = 2, Name = "c2"},
new { Id = 2, Name = "c3"},
new { Id = 1, Name = "c4"},
new { Id = 3, Name = "c5"},
};
var grouped = list.GroupBy(x => x.Id);
lambda 函数 x => x.Id
可以根据您的需要进行定制
https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.groupby?view=netcore-3.1
我正在尝试将此函数从 Javascript 转换为 C#。 目标是接收合同列表并将它们分类为子组,如果它们具有相同的客户,则将它们捆绑在一起。
> let c1 = {id:1,name:"c1"}
> let c2 = {id:2,name:"c2"}
> let c3 = {id:2,name:"c3"}
> let c4 = {id:1,name:"c4"}
> let c5 = {id:3,name:"c5"}
> let list = [c1,c2,c3,c4,c5]
>
> const groupByKey = (array, property) => { return
> array.reduce((accumulator, object) => {
> const key = object[property];
>
> if (!accumulator[key]) {
> accumulator[key] = []
> }
> accumulator[key].push(object)
> return accumulator; }, {}) }
>
> groupByKey(list, "id")
>
> // input = let list = [c1,c2,c3,c4,c5] //output =
> {"1":[{"id":1,"name":"c1"},{"id":1,"name":"c4"}],"2":[{"id":2,"name":"c2"},{"id":2,"name":"c3"}],"3":[{"id":3,"name":"c5"}]}
// 最好输出是包含捆绑合同的列表列表。
更新: 我已经编写了函数,问题是我需要 return 只是合同 ID,而不是整个对象。
desired output [[c1.id,c4.id],[c2.id,c3.id],[c5.id]]
这是我目前拥有的。
public JsonResult checkForSameClient(long[] listOfContracts)
{
// here i grab the list of contracts ids then go to my repository and find //the whole objects to be compared.
IQueryable<ContratEspace> contrats = contratEspaceRepository.Get(1, listOfContracts.Count(), listOfContracts);
var contratList = contrats.ToList();
var finalArray = contrats.GroupBy(c => c.id_clientGestion).ToList();
return new JsonResult() { Data = finalArray };
}
但这不是 return 正确 Json.Data 我有错误:
SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at fromJson (angular.js:1282)
at defaultHttpResponseTransform (angular.js:10133)
at angular.js:10224
at forEach (angular.js:321)
at transformData (angular.js:10223)
at transformResponse (angular.js:10996)
at processQueue (angular.js:15552)
at angular.js:15568
at Scope.$eval (angular.js:16820)
由于 GroupBy 内置于 .NET 中,因此无需编写聚合函数。
如何在 C# 中对列表进行分组:
var list = new [] {
new { Id = 1, Name = "c1"},
new { Id = 2, Name = "c2"},
new { Id = 2, Name = "c3"},
new { Id = 1, Name = "c4"},
new { Id = 3, Name = "c5"},
};
var grouped = list.GroupBy(x => x.Id);
lambda 函数 x => x.Id
可以根据您的需要进行定制
https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.groupby?view=netcore-3.1