Is there a way to reset count after select? Here is sample in Fiddle.
This is the result I'm getting,
{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'}
This is what I've tried.
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;
}
And this is what I want to achieve. Since MonthYear value is different I want to reset count back to 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'}
CodePudding user response:
If you group by only month, you could use the .GroupBy()
's resultSelector
to create a DynamicProjectCalendarAug
object for each group item. The resultSelector
takes advantage of the .Select()
overload that provides the index of the source element. It would look like the following:
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();
For a simplified implementation of your classes, this example input:
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"}
};
will have the following output:
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
Example fiddle here.
CodePudding user response:
Created an example to show how you can solve this. https://dotnetfiddle.net/b9mnau
CodePudding user response:
Having
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"),
};
you could add group row id the following ways:
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)));
The second method uses the lesser known variant of Select
which provides an index.