C
C#14mo ago
SWEETPONY

❔ how remove duplicates from string?

I have following method:
private IEnumerable<(string Title, string Date)> GetFormattedHolidays(CalendarDto calendarDto)
{
foreach (var holiday in calendarDto.Holidays)
{
foreach (var day in calendarDto.Days)
{
if (day.Holiday == holiday.Id.ToString())
{
var date = day.Day?.ToString("dd MMMM", _context.Culture);

yield return ($"{holiday.Title}", $"{date}");
}
}
}
}
private IEnumerable<(string Title, string Date)> GetFormattedHolidays(CalendarDto calendarDto)
{
foreach (var holiday in calendarDto.Holidays)
{
foreach (var day in calendarDto.Days)
{
if (day.Holiday == holiday.Id.ToString())
{
var date = day.Day?.ToString("dd MMMM", _context.Culture);

yield return ($"{holiday.Title}", $"{date}");
}
}
}
}
it returns: [ "01 january, 02 january, 03 january", "07 march, 08 march" ] but I need this: [ "01, 02, 03 january", "07, 08 march" ] how to do it?
18 Replies
Doombox
Doombox14mo ago
you can try using a GroupBy statement to group them by the month then iterate each group to produce the string
SWEETPONY
SWEETPONY14mo ago
this is very inconvenient to do..
Doombox
Doombox14mo ago
yup
SWEETPONY
SWEETPONY14mo ago
idk maybe regex
Doombox
Doombox14mo ago
that's definitely more inconvenient var grouped = calendarDto.Days.GroupBy(x => x.Month); isn't that inconvenient working with it is slightly more work but not hugely so I assume holidays can span across months? if so that really would be the way to go, you could write a "simpler" solution if a single holiday was constrained to one month
SWEETPONY
SWEETPONY14mo ago
I will try, thank you
Pobiega
Pobiega14mo ago
100% the trick is to group them before converting stuff to strings
Angius
Angius14mo ago
Should be doable entirely with LINQ
SWEETPONY
SWEETPONY14mo ago
I don't think that we can group by month my objects:
internal record CalendarDto(
List<DayDto> Days,
short Year,
List<HolidayDto> Holidays);

internal record DayDto(
DateTime? Day,
DateTime? From,
string Type,
string Holiday);

internal record HolidayDto(
int Id,
string Title);
internal record CalendarDto(
List<DayDto> Days,
short Year,
List<HolidayDto> Holidays);

internal record DayDto(
DateTime? Day,
DateTime? From,
string Type,
string Holiday);

internal record HolidayDto(
int Id,
string Title);
hmmm
Doombox
Doombox14mo ago
your DateTime will contain a month property you can use to group them by
Pobiega
Pobiega14mo ago
var grouped = data.Days
.GroupBy(x => x.Day.Value.Month)
.Select(x =>
$"{string.Join(", ", x.Select(y => y.Day.Value.Day))} {CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(x.Key)}")
.ToArray();

Console.WriteLine(string.Join("\n", grouped));
var grouped = data.Days
.GroupBy(x => x.Day.Value.Month)
.Select(x =>
$"{string.Join(", ", x.Select(y => y.Day.Value.Day))} {CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(x.Key)}")
.ToArray();

Console.WriteLine(string.Join("\n", grouped));
ez katka
1, 2, 3 January
6, 7, 8 March
1, 2, 3 January
6, 7, 8 March
SWEETPONY
SWEETPONY14mo ago
soo good, thanks I definitely have to learn how to write good linq queries may you give me some advices about linq? how to write good linq queries? (as yours)
Pobiega
Pobiega14mo ago
you need to really understand the power of Select really. thats like, 90% of it my thought process here was...
okay, lets group the days by month, then I need to separate the days (values) from the month (key) into a string. for the days, we just join them up (but days are not strings, so we "extract" the string we want with another select) for the month, we use CultureInfo to get the month name because our month key is an int
SWEETPONY
SWEETPONY14mo ago
oookey, I see! thanks for helping, I'll practice linq to write queries as good as you Ok
Pobiega
Pobiega14mo ago
Select is often called Map in other languages. SelectMany is often called Bind
SWEETPONY
SWEETPONY14mo ago
ah it's like map and bind from haskell
Pobiega
Pobiega14mo ago
yes
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.