So, I am using the Linq entity framework. I have 2 entities: Content and Tag. They are in a many-to-many relationship with one another. Content can have many Tags and Tag can have many Contents. So I am trying to write a query to select all contents where any tags names are equal to blah
The entities both have a collection of the other entity as a property(but no IDs). This is where I am struggling. I do have a custom expression for Contains (so, whoever may help me, you can assume that I can do a ‘contains’ for a collection). I got this expression from: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2670710&SiteID=1
After reading about the PredicateBuilder, reading all of the wonderful posts that people sent to me, posting on other sites, and then reading more on Combining Predicates and Canonical Function Mapping.. oh and I picked up a bit from Calling functions in LINQ queries (some of these classes were taken from these pages).
I FINALLY have a solution!!! Though there is a piece that is a bit hacked…
Let’s get the hacked piece over with 🙁
I had to use reflector and copy the ExpressionVisitor class that is marked as internal. I then had to make some minor changes to it, to get it to work. I had to create two exceptions (because it was newing internal exceptions. I also had to change the ReadOnlyCollection() method’s return from:
To:
I would post the class, but it is quite large and I don’t want to clutter this post any more than it’s already going to be. I hope that in the future that class can be removed from my library and that Microsoft will make it public. Moving on…
I added a ParameterRebinder class:
Then I added a ExpressionExtensions class:
And the last class I added was PredicateBuilder:
This is my result… I was able to execute this code and get back the resulting ‘content’ entities that have matching ‘tag’ entities from the tags that I was searching for!
Please let me know if anyone needs help with this same thing, if you would like me to send you files for this, or just need more info.