❔ how remove duplicates from string?
I have following method:
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
you can try using a
GroupBy
statement to group them by the month
then iterate each group to produce the stringthis is very inconvenient to do..
yup
idk maybe regex
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 monthI will try, thank you
100% the trick is to group them before converting stuff to strings
Should be doable entirely with LINQ
I don't think that we can group by month
my objects:
hmmm
your
DateTime
will contain a month property you can use to group them by
ez katka
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)
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
oookey, I see!
thanks for helping, I'll practice linq to write queries as good as you
Select
is often called Map
in other languages. SelectMany
is often called Bind
ah it's like map and bind from haskell
yes
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.