❔ Need help with a LINQ query
Hi,
I could use a bit of help building a LINQ query.
Assume a simple entity like this:
Then you have an IEnumerable<Event>. From this collection I want to extract all customer ids that only have eventTypeId == 1 associated with them.
So if there are two events for one customerId, and those two eventTypeIds are 1 and 2 then I don't want that associated customer id.
If both eventTypeIds are 1 then I want that specific customerId. Does that make sense?
Currently I have this
But looking at it it doesn't really make sense. And it doesn't return what I thought it would. Any ideas?
13 Replies
Something like this should work
Ah, wait, you want events with typeid 2 as well
Oh hang on I think I mixed up my instruction 🙂
I'll verify it, hang on a bit..
This code only gives you ones that have
EventTypeId
equal to 1
, and deduplicates it by customer ID
Would this imply that there may exist events with EventTypeId == 2 for a given customerId, but that we just filter them out?
Because if they exist in the collection, then I can't use that specific customerId
It gets all events where
EventTypeId
is 1
Doesn't care about customer IDsOk, I think we need to be more specific.
A customerId may have several events associated with them. Several 1s or several 2s or a mix of both.
If there is any 2 at all, then all of the 1s for that customer id are unusable.
Ah, gotcha
You'd need to group by user ID first, then
Ah, okay
Then, in each group, check if an event with type ID
1
exists
And filter accordingly
So something like this?
Something like that, yeah
i wish linq had a
None
, but i understand why it doesn'tWas 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.