重置计数 - 如果使用 C# lambda 表达式在特定列中有不同的值
Reset Count - If different value in specific column using C# lambda expression
在 select 之后有没有办法重置计数?这是 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'}
创建了一个示例来说明如何解决这个问题。
https://dotnetfiddle.net/b9mnau
有
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
变体,它提供了一个索引。
如果仅按月份分组,则可以使用提供源元素索引的 .GroupBy()
's resultSelector
to create a DynamicProjectCalendarAug
object for each group item. The resultSelector
takes advantage of the .Select()
overload。它看起来像下面这样:
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
示例 fiddle here.
在 select 之后有没有办法重置计数?这是 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'}
创建了一个示例来说明如何解决这个问题。 https://dotnetfiddle.net/b9mnau
有
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
变体,它提供了一个索引。
如果仅按月份分组,则可以使用提供源元素索引的 .GroupBy()
's resultSelector
to create a DynamicProjectCalendarAug
object for each group item. The resultSelector
takes advantage of the .Select()
overload。它看起来像下面这样:
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
示例 fiddle here.