有沒有辦法在選擇后重置計數?這是Fiddle中的示例。
這是我得到的結果,
{groupRowId: 1, monthYear: '2020-08', projectName: 'Project 1'}
{groupRowId: 2, monthYear: '2020-08', projectName: 'Project 2'}
{groupRowId: 3, monthYear: '2020-08', projectName: 'Project 3'}
{groupRowId: 4, monthYear: '2021-08', projectName: 'Project 4'}
{groupRowId: 5, monthYear: '2021-08', projectName: 'Project 5'}
{groupRowId: 6, monthYear: '2021-08', projectName: 'Project 6'}
這是我嘗試過的。
public List<object> GenerateDynamicCalendar(List<ProjectResult> result) {
int row = 1;
var queryAug = result.Where(c => c.LiveDate != Convert.ToDateTime("1900-01-01 00:00:00.000") && c.LiveDate.Month == 8)
.GroupBy(c => new { c.Month, c.Name})
.Select(g => new DynamicProjectCalendarAug
{
GroupRowId = row ,
MonthYear = g.Key.Month,
ProjectName = g.Key.Name,
Aug = g.Where(c => c.LiveDate != Convert.ToDateTime("1900-01-01 00:00:00.000") && c.LiveDate.Month == 8).Max(c => c.Name)
}).ToList();
List<object> query = otherMonthResult.Cast<object>()
.Concat(queryAug)
return query;
}
這就是我想要實作的目標。由于MonthYear值不同,我想將計數重置回 1
{groupRowId: 1, monthYear: '2020-08', projectName: 'Project 1'}
{groupRowId: 2, monthYear: '2020-08', projectName: 'Project 2'}
{groupRowId: 3, monthYear: '2020-08', projectName: 'Project 3'}
{groupRowId: 1, monthYear: '2021-08', projectName: 'Project 4'}
{groupRowId: 2, monthYear: '2021-08', projectName: 'Project 5'}
{groupRowId: 3, monthYear: '2021-08', projectName: 'Project 6'}
uj5u.com熱心網友回復:
如果您只按月份分組,您可以使用.GroupBy()'s為每個分組項resultSelector創建一個物件。DynamicProjectCalendarAug它resultSelector利用了提供源元素索引的.Select()多載。它如下所示:
var queryAug = result
//.Where( ... )
.GroupBy(
resultItem => resultItem.Month,
( _, itemsInMonth ) => itemsInMonth
.Select(( item, i ) => new DynamicProjectCalendarAug {
GroupRowId = i 1,
MonthYear = item.Month,
ProjectName = item.Name,
//Aug = ...
}))
.SelectMany(_ => _)
.ToList();
對于您的類的簡化實作,此示例輸入:
List<ProjectResult> result = new()
{
new() { Month = "2020-08", Name = "Project 1"},
new() { Month = "2020-08", Name = "Project 2"},
new() { Month = "2020-08", Name = "Project 3"},
new() { Month = "2021-08", Name = "Project 4"},
new() { Month = "2021-08", Name = "Project 5"},
new() { Month = "2021-08", Name = "Project 6"}
};
將有以下輸出:
1 | 2020-08 | Project 1
2 | 2020-08 | Project 2
3 | 2020-08 | Project 3
1 | 2021-08 | Project 4
2 | 2021-08 | Project 5
3 | 2021-08 | Project 6
示例小提琴在這里。
uj5u.com熱心網友回復:
創建了一個示例來展示如何解決這個問題。 https://dotnetfiddle.net/b9mnau
uj5u.com熱心網友回復:
有
record WithRowId(int GroupRowId, MyDto MyDto);
record MyDto(int X, string Val);
var list = new []{
new MyDto(X: 1, Val: "a"),
new MyDto(X: 1, Val: "b"),
new MyDto(X: 2, Val: "c"),
new MyDto(X: 2, Val: "d"),
};
您可以通過以下方式添加組行 ID:
var list2 = new List<WithRowId>();
foreach( var group in list.GroupBy( c => c.X ))
{
int i = 0;
foreach(var r in group)
{
list2.Add(new WithRowId(1 i , r));
}
}
// Method 2
var list2B = list
.GroupBy( c => c.X)
.SelectMany( g => g.Select((x, i) => new WithRowId(GroupRowId: i 1, MyDto: x)));
Select第二種方法使用提供索引的鮮為人知的變體。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/442856.html
下一篇:在多個值上相交兩個物件串列
