Answered by:
Complete LINQ query where there is a number of user ids where it equals a certain #

Question
-
User-1188570427 posted
I need to pull in all user ids where the count is equal to the number of timezones in the list:
------------
DTO:
int RemovedUserId
string time zone
------------
My Timezone List is:
US/Eastern
US/Central
The timezone list count is 2
So I need to pull all user ids (so that I can remove them from a list) where the count is 2
So if this was my DTO list (user id / timezone)
- 2 / Us Eastern
- 3 / Us Central
- 2 / Us Central
- 4 / Us Eastern
So the final answer would be User Id 2 because there are 2 records in the list
3 and 4 would be excluded because there is only one.
Is there a way to do this in LINQ?
Saturday, January 26, 2019 6:27 AM
Answers
-
User-1188570427 posted
Keep in mind that we cannot see the table schema or data and it seems the intent is filtering UserId by a count which makes no sense.
Can you consult with coworkers?
I gave the data in the post? This isn't a database table either.
Yea, it does make sense for what I needed:
I figured it out:
var userIdsToRemove = (from theList in list group theList by theList.RemoveUserId into listGroup select new { UserId = listGroup.Key, Count = listGroup.Count(), }).Where(w => w.Count == totalCount) .Select(s => s.UserId) .ToList(); var timezonesToRemove = (from theList in list group theList by theList.RemoveNodaTimezoneNm into listGroup select new { NodaTimeZone = listGroup.Key, Count = listGroup.Count(), }).Where(w => w.Count != totalCount) .Select(s => s.NodaTimeZone) .ToList();
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, January 26, 2019 4:16 PM
All replies
-
User475983607 posted
Keep in mind that we cannot see the table schema or data and it seems the intent is filtering UserId by a count which makes no sense.
Can you consult with coworkers?
Saturday, January 26, 2019 1:40 PM -
User-1188570427 posted
Keep in mind that we cannot see the table schema or data and it seems the intent is filtering UserId by a count which makes no sense.
Can you consult with coworkers?
I gave the data in the post? This isn't a database table either.
Yea, it does make sense for what I needed:
I figured it out:
var userIdsToRemove = (from theList in list group theList by theList.RemoveUserId into listGroup select new { UserId = listGroup.Key, Count = listGroup.Count(), }).Where(w => w.Count == totalCount) .Select(s => s.UserId) .ToList(); var timezonesToRemove = (from theList in list group theList by theList.RemoveNodaTimezoneNm into listGroup select new { NodaTimeZone = listGroup.Key, Count = listGroup.Count(), }).Where(w => w.Count != totalCount) .Select(s => s.NodaTimeZone) .ToList();
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, January 26, 2019 4:16 PM