So I have a Blog object which has a list of tag objects (List<Tag>).
I’m trying to create a method that takes a list of tags and returns a list of blogs that contain all the tags in the passed in list.
I was able to make a method that will return a list of blogs if it matches one tag, but not a list of tags.
to do that I have this
entities.Blogs.Where(b => b.Tags.Any(t => t.Name == tagName))
But I can’t figure out how to do something like this
entities.Blogs.Where(b => b.Tags.Any(t => t.Name == tags[0] AND t.Name == tags[1] AND t.Name == tags[2] etc.......))
Is there any way to do this?
Thank you!
I’m using LINQ to Entities
Logically, I think you want something like:
Alternatively:
If this is using LINQ to Entities, I doubt that this will work – but it should work if you’re just using LINQ to Objects. Even then, it’s not going to be terribly efficient. I suspect there’s a more efficient way of doing things, but I can’t immediately think of it… it feels like you want a join, but then it gets tricky again.