C
C#2y ago
malkav

❔ How to populate my IGrouping with null values??

So I have this way of grouping items, and I'm a little stuck. I have two classes, one is the child of another. This is the model of data from the Database.
public class Hours
{
// Other props
public int Month {get;set;}
public int Year {get;set;}
public List<Days> WorkedDays {get;set;}
}

public class Days
{
public DateTime WorkedDay {get;set}
// Other properties
}
public class Hours
{
// Other props
public int Month {get;set;}
public int Year {get;set;}
public List<Days> WorkedDays {get;set;}
}

public class Days
{
public DateTime WorkedDay {get;set}
// Other properties
}
Now what I want to do with this data, is render it in a <table> (in Blazor), but grouped by week. However, what I have now, is that it only populates the existing items, but what I want is for the table to look like the image (For <example> in the image, see second image) Now I have the items the user has added to their List<Days> grouped by week number with the following piece of code (Thanks to someone on this server):
Hours selected = _allHours.Find(m => m.Month == (int)_selectedMonth && m.Year == _selectedYear);

var grouped = selected.WorkedDays.GroupBy(d => CultureInfo.CurrentCulture.Calendar.GetWeekOfyear(d.WorkedDay, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
Hours selected = _allHours.Find(m => m.Month == (int)_selectedMonth && m.Year == _selectedYear);

var grouped = selected.WorkedDays.GroupBy(d => CultureInfo.CurrentCulture.Calendar.GetWeekOfyear(d.WorkedDay, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
But what I don't know now, and why I came to you awesome guys, is to help me figure out how I am going to populate the IEnumerable<IGrouped<int, Hours>> with null values for each of the Dates in my Hours.WorkedDays (List<Days>) And then render an empty container for the days that have "null" in them, and render the already existing items in the days of the week they belong to, but both having the option to add an item to that specific day. And afterwards, how am I going to render the entire grouping back into a List so I can push that back into the Hours class, and update the database. Hope my question isnt too vague..
2 Replies
malkav
malkavOP2y ago
as addition: Here is my blazor template for the table to render:
<table class="table-borderless">
<tr>
@foreach (IGrouping<int, DaysWorked> list in _group)
{
<thead>
<tr><th colspan="2" class="text-center">Week #@(list.Key)</th></tr>
</thead>
<tbody>
@foreach (DaysWorked item in list)
{
<tr>
<th>@item.DayWorked.Day / @item.DayWorked.Month</th>
<td>@* TODO: Create form of "add item here" *@ Data</td>
</tr>
}
</tbody>
}
</tr>
</table>
<table class="table-borderless">
<tr>
@foreach (IGrouping<int, DaysWorked> list in _group)
{
<thead>
<tr><th colspan="2" class="text-center">Week #@(list.Key)</th></tr>
</thead>
<tbody>
@foreach (DaysWorked item in list)
{
<tr>
<th>@item.DayWorked.Day / @item.DayWorked.Month</th>
<td>@* TODO: Create form of "add item here" *@ Data</td>
</tr>
}
</tbody>
}
</tr>
</table>
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server