Andy Jones
Andy Jones

Reputation: 869

Linq - Narrow down a list object of events by tags within

I'm a little bit stumped with this Linq statement. My basic database structure is:

I have a list of events, and I want to narrow it down by events that either contain the specified tag, or the venue they are held at contain the tag.

I have tried this, and I think I may not be too far off, but I'm still struggling to get it to work:

eventsList = eventsList
                .SelectMany(x => x.EventTag)
                .Where(et => et.TagID == tagID)
                .Select(et => t.Event)
                .Union(eventsList.SelectMany(x=>x.Venue.VenueTag)
                .Where(vt =>vt.TagID == tagID)
                .Select(vt=>vt.Venue.Event)
                .Distinct()
                .ToList();

I'd be very grateful for any help!

Andy

Upvotes: 1

Views: 230

Answers (1)

Andrei
Andrei

Reputation: 56716

You can use Any for this:

eventsList.Where(
              x => x.EventTag.Any(et => et.TagID == tagID)
                   || x.Venue.Any(v => v.Any(vt => vt.TagID == tagID)))
          .ToList();

Upvotes: 3

Related Questions