C
C#2y ago
Natty

❔ LINQ Question - Thought I solved it...

I have a list of dates in month/day/year format, and I've created a hashset that holds ending quarter months:
var allDates = List(); // "1/1/2017, 5/23/2018, etc"
var quarterEndMonths = new HashSet<string> { "3", "6", "9", "12" };
var allDates = List(); // "1/1/2017, 5/23/2018, etc"
var quarterEndMonths = new HashSet<string> { "3", "6", "9", "12" };
My goal is to have a bool that checks if the allDates only contains Quarter months (3,6,9,12). Here's what I have so far:
bool onlyContainsQuarters = allDates.Any(x => quarterEndMonths.Any(prefix => x.StartsWith(prefix)));
bool onlyContainsQuarters = allDates.Any(x => quarterEndMonths.Any(prefix => x.StartsWith(prefix)));
21 Replies
FusedQyou
FusedQyou2y ago
What type is allDates?
Becquerel
Becquerel2y ago
I think you want All instead of Any
FusedQyou
FusedQyou2y ago
That too If allDates is of type Date you can also just get the month from it instead of doing that prefix check. It's not related but either way.
Becquerel
Becquerel2y ago
i'm gonna guess they're strings (unfortunately)
FusedQyou
FusedQyou2y ago
And if you want to make it more complex, check if month % 4 == 0 😛 That gets rid of your quarterEndMonths field
Natty
NattyOP2y ago
sorry of type string Oo wait, you're right, that would work with a string tho?
FusedQyou
FusedQyou2y ago
No because you still need to get the prefix, which can either be 1 character or two I would suggest you switch to a Date type or parse the strings when the application starts You can use linq to call Select on the field and then use Date.(try)Parse on each entry
Natty
NattyOP2y ago
I forgot to mention that sometimes the allDates would only contain year strings 2017, etc.
FusedQyou
FusedQyou2y ago
You can use tryParse and just ignore invalid entries if you want? Or do you need to explicitly return false on those?
Natty
NattyOP2y ago
Eh I'm checking format for our data when it's in monthly/quarterly/yearly. So if user selects monthly frequency, then i'm checking to ensure all their data is in monthly format, etc. I think I'll just go with the All() So that is:
bool onlyContainsQuarters = allDates.All(x => quarterEndMonths.Any(prefix => x.StartsWith(prefix)));
bool onlyContainsQuarters = allDates.All(x => quarterEndMonths.Any(prefix => x.StartsWith(prefix)));
Correct? Still need the .Any() for the set?
FusedQyou
FusedQyou2y ago
All basically fixes your issue. I was trying to make a bit more sense of the situation 😛 If you're sure month/day/year is always the format used, not much can go wrong
Natty
NattyOP2y ago
Yes, it is. small app.
FusedQyou
FusedQyou2y ago
Just remember that if somebody sends 34739812649081234 as the date it will also be valid, because it starts with 3
Natty
NattyOP2y ago
Just want to make sure I have the All/Any query above correct.
FusedQyou
FusedQyou2y ago
So if you want to avoid that, trying to parse a date before checking the prefix is still a good idea
Natty
NattyOP2y ago
Yeah, great point. We have some other prevents there where this would only be pulling from the dates column, and burden is on the client for this initial upload section. Later when we process calculations, i do a TryParse()
FusedQyou
FusedQyou2y ago
Then it would be: bool onlyContainsQuarters = allDates.All(x => Date.TryParse(x, out _) && quarterEndMonths.Any(prefix => x.StartsWith(prefix))); I see, just remember client validation can be omitted If it's a small app it's not as big of a deal I suppose
Natty
NattyOP2y ago
Ah ok that does make sense...
FusedQyou
FusedQyou2y ago
Forgot to add the out part. Fixed now.
Natty
NattyOP2y ago
Thank you. I'm going to roll with just the:
bool onlyContainsQuarters = allDates.All(x => quarterEndMonths.Any(prefix => x.StartsWith(prefix)));
bool onlyContainsQuarters = allDates.All(x => quarterEndMonths.Any(prefix => x.StartsWith(prefix)));
For now, but may actually update to the date check because the does make more sense...
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