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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:35:54+00:00 2026-06-17T16:35:54+00:00

I’m using PredicateBuilder to create a dynamic Where clause to query data from a

  • 0

I’m using PredicateBuilder to create a dynamic Where clause to query data from a DataTable. I have a Dictionary that contains my column names and values I need to search for. I’m simply iterating over the dictionary, if the key matches a column name, then add that key and value to the predicate. Everything seems to work fine until the actual query is run against the datatable, I get zero records back:( But if I replace the dynamic predicate with something like p => p[“Year”] == “2010”, I get records back. Here’s the code:

var objectList = table.AsEnumerable();
Func<DataRow, bool> predicate = GetPredicate(parms, table.Columns);

var list1 = objectList.Where(predicate).ToList();

private static Func<DataRow, bool> GetPredicate(Dictionary <string, string> parms, DataColumnCollection dataColumnCollection)
    {
        var predicate = PredicateBuilder.False<DataRow>();
        foreach (var parm in parms)
        {
            if (dataColumnCollection.Contains(parm.Key))
            {
                var copy = parm;
                predicate = predicate.And(p => p[copy.Key] == copy.Value);
            }
        }
        return predicate.Compile();
    }

Any help would be greatly appreciated:)

  • 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-06-17T16:35:55+00:00Added an answer on June 17, 2026 at 4:35 pm

    You’re starting with false and then adding and clauses.

    false && ... always returns false, so your Where clause will never match anything.

    Try starting with:

    var predicate = PredicateBuilder.True<DataRow>();
    

    ALSO:

    You are closing over a loop variable, which means all your predicates will use the last parm in parms.

    You can fix this by creating a local copy in your loop:

    foreach( var parm in parms )
    {
      if (dataColumnCollection.Contains(parm.Key))
      {
        var copy = parm;
        predicate = predicate.And( p => p[ copy.Key ] == copy.Value );
      }
    

    UPDATE:

    DataRow[ key ] is of type object, so in p[ copy.Key ] == copy.Value, the equality operator is object reference equality, not string equality.

    You can fix this by specifying String.Equals:

    predicate = predicate.And( p => String.Equals( p[ copy.Key ], copy.Value ) );
    

    Interestingly, this example shows that you can have multiple instances of string with the same contents.

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

Sidebar

Related Questions

I am using jsonparser to parse data and images obtained from json response. When
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to

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.