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 6109915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:27:27+00:00 2026-05-23T14:27:27+00:00

I have two similar methods that take a criteria object (dumb object with a

  • 0

I have two similar methods that take a criteria object (dumb object with a list of properties), call a “CreateExpression” method on that criteria object, and then use the returned expression to filter results.

One of my examples has an argument of only one criteria, and it works with no problem. My second method takes a List<Criteria> and then attempts to iterate through each criteria in the list, and generate the expression for it, and then “and” it to the previous expression. The end result is supposed to be one big expression that I can then use in my linq query.

However, this second method is not working. When I use the debugger, I can see the predicate with it’s body and lambda expression internally, but when it hits the SQL server, all it sends is a select statement with no where clauses at all.

Here is the method that works (with one criteria object):

public static List<Segment> GetByCriteria(Criteria.SegmentCriteria myCriteria)
        {
            List<Segment> result = null;

            List<Segment> qry = db.Segments.AsExpandable<Segment>().Where<Segment>(CreateCriteriaExpression(myCriteria)).ToList<Segment>();
            qry = qry.Where<Segment>(CreateCriteriaExpressionForCustomProperties(myCriteria).Compile()).ToList<Segment>();


            if (qry != null && qry.Count != 0)
            {
                result = qry;
            }

            return result;
        }

Here is the one that doesn’t work:

public static List<Segment> GetByCriteria(List<Criteria.SegmentCriteria> myCriteria, Common.MultipleCriteriaMatchMethod myMatchMethod)
        {

            List<Segment> result = null;

            var predicate = PredicateBuilder.True<Segment>();
            var customPropertiesPredicate = PredicateBuilder.True<Segment>();


            foreach (Criteria.SegmentCriteria x in myCriteria)
            {
                if (myMatchMethod == Common.MultipleCriteriaMatchMethod.MatchOnAll)
                {
                    predicate = predicate.And(CreateCriteriaExpression(x).Expand());
                    customPropertiesPredicate = customPropertiesPredicate.And(CreateCriteriaExpressionForCustomProperties(x).Expand());
                }
                else if (myMatchMethod == Common.MultipleCriteriaMatchMethod.MatchOnAny)
                {
                    predicate = predicate.Or(CreateCriteriaExpression(x).Expand());
                    customPropertiesPredicate = customPropertiesPredicate.Or(CreateCriteriaExpressionForCustomProperties(x).Expand());
                }
            }


            List<Segment> qry = db.Segments.AsExpandable<Segment>().Where<Segment>(predicate.Expand()).ToList<Segment>();
            qry = qry.Where<Segment>(customPropertiesPredicate.Expand().Compile()).ToList<Segment>();

            if (qry != null && qry.Count != 0)
            {
                result = qry;
            }

            return result;
        }

I am using Predicate Builder to generate the initial expression. I don’t believe there is a problem with those methods since they work with the first (singular) method.

Does anyone know what’s going on here?

Edit
I forgot to say that the backend is entity framework.

  • 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. Editorial Team
    Editorial Team
    2026-05-23T14:27:28+00:00Added an answer on May 23, 2026 at 2:27 pm

    Ok, I figured it out.

    I needed to use the “Expand()” method on ALL places where I was calling a predicate in the lines where I was appending the predicate. Here is the new fixed version of the foreach loop in my second method:

    foreach (Criteria.SegmentCriteria x in myCriteria)
                {
                    Criteria.SegmentCriteria item = x;
                    if (myMatchMethod == Common.MultipleCriteriaMatchMethod.MatchOnAll)
                    {
                        predicate = predicate.Expand().And<Segment>(CreateCriteriaExpression(item).Expand());
                        customPropertiesPredicate = customPropertiesPredicate.Expand().And<Segment>(CreateCriteriaExpressionForCustomProperties(item).Expand());
                    }
                    else if (myMatchMethod == Common.MultipleCriteriaMatchMethod.MatchOnAny)
                    {
                        predicate = predicate.Expand().Or<Segment>(CreateCriteriaExpression(item).Expand());
                        customPropertiesPredicate = customPropertiesPredicate.Expand().Or<Segment>(CreateCriteriaExpressionForCustomProperties(item).Expand());
                    }
                }
    

    And now it works! I found details about this also on this other question.

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

Sidebar

Related Questions

I have the following two methods that (as you can see) are similar in
I have two extension methods that are very similar. I'd like to remove the
I had two seperated projects that used the same database, similar methods, etc. So
I have two very similar methods: public IQueryable<User> Find(Func<User, bool> exp) { return db.Users.Where(exp);
I have two very similar methods in Grails, something like calculate statistics by os
assume that i have two sequential methods, methodA and methodB ( methodB is called
I have two very similar classes that do essentially the same thing. The only
I have two pieces of code which are very similar in that they register
I have two similar codes. The first one works as expected. urlpatterns = patterns('',
I have two tables with similar columns - let's say table A with column

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.