Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 50561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:36:45+00:00 2026-05-10T16:36:45+00:00

So, I am using the Linq entity framework. I have 2 entities: Content and

  • 0

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

Edit 1

I ended up finding my own answer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T16:36:46+00:00Added an answer on May 10, 2026 at 4:36 pm

    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:

    return sequence.ToReadOnlyCollection<Expression>(); 

    To:

    return sequence.AsReadOnly(); 

    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:

    public class ParameterRebinder : ExpressionVisitor {         private readonly Dictionary<ParameterExpression, ParameterExpression> map;          public ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map) {             this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>();         }          public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp) {             return new ParameterRebinder(map).Visit(exp);         }          internal override Expression VisitParameter(ParameterExpression p) {             ParameterExpression replacement;             if (map.TryGetValue(p, out replacement)) {                 p = replacement;             }             return base.VisitParameter(p);         }     } 

    Then I added a ExpressionExtensions class:

    public static class ExpressionExtensions {         public static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge) {             // build parameter map (from parameters of second to parameters of first)             var map = first.Parameters.Select((f, i) => new { f, s = second.Parameters[i] }).ToDictionary(p => p.s, p => p.f);              // replace parameters in the second lambda expression with parameters from the first             var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);              // apply composition of lambda expression bodies to parameters from the first expression              return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);         }          public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second) {             return first.Compose(second, Expression.And);         }          public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second) {             return first.Compose(second, Expression.Or);         }     } 

    And the last class I added was PredicateBuilder:

    public static class PredicateBuilder {     public static Expression<Func<T, bool>> True<T>() { return f => true; }     public static Expression<Func<T, bool>> False<T>() { return f => false; }  } 

    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!

        public static IList<Content> GetAllContentByTags(IList<Tag> tags) {         IQueryable<Content> contentQuery = ...          Expression<Func<Content, bool>> predicate = PredicateBuilder.False<Content>();          foreach (Tag individualTag in tags) {             Tag tagParameter = individualTag;             predicate = predicate.Or(p => p.Tags.Any(tag => tag.Name.Equals(tagParameter.Name)));         }          IQueryable<Content> resultExpressions = contentQuery.Where(predicate);          return resultExpressions.ToList();     } 

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 96k
  • Answers 96k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Clearing a list in place will affect all other references… May 11, 2026 at 7:11 pm
  • Editorial Team
    Editorial Team added an answer import re infile = open('C:/infile.txt') outfile = open('C:/outfile.txt', 'w') pattern… May 11, 2026 at 7:11 pm
  • Editorial Team
    Editorial Team added an answer Here's a list of open source .Net profilers. I have… May 11, 2026 at 7:11 pm

Related Questions

I am a little ashamed to say that I have never used an ORM;
I am new to Entity Framework, and ORM's for that mather. In the project
So I am working on a project that uses a ASP.NET server and we
I've just started learning how to use the Entity Framework to write a very

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.