C
C#2y 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
Doombox2y ago
you can try using a GroupBy statement to group them by the month then iterate each group to produce the string
SWEETPONY
SWEETPONYOP2y ago
this is very inconvenient to do..
Doombox
Doombox2y ago
yup
SWEETPONY
SWEETPONYOP2y ago
idk maybe regex
Doombox
Doombox2y 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
SWEETPONYOP2y ago
I will try, thank you
Pobiega
Pobiega2y ago
100% the trick is to group them before converting stuff to strings
Angius
Angius2y ago
Should be doable entirely with LINQ
SWEETPONY
SWEETPONYOP2y 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
Doombox2y ago
your DateTime will contain a month property you can use to group them by
Pobiega
Pobiega2y 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
SWEETPONYOP2y 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
Pobiega2y 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
SWEETPONYOP2y ago
oookey, I see! thanks for helping, I'll practice linq to write queries as good as you Ok
Pobiega
Pobiega2y ago
Select is often called Map in other languages. SelectMany is often called Bind
SWEETPONY
SWEETPONYOP2y ago
ah it's like map and bind from haskell
Pobiega
Pobiega2y ago
yes
Accord
Accord2y 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.
Want results from more Discord servers?
Add your server