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

  • Home
  • SEARCH
  • 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 328241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:28:36+00:00 2026-05-12T09:28:36+00:00

i want to create the following query in expression trees: var test = from

  • 0

i want to create the following query in expression trees:

var test = from datarow in tempResults
           where datarow.Field<String>("ColumnName") == "Column"
           select datarow;

How do i create the expression:

datarow.Field<String>("ColumnName")?

i tried everything, i even got stuck on getting the MethodInfo of Field for the Expression.Call method.
Field is an extention Method of DataRowExtentions.

Do i have to use Expression.Call() for this?
How do i get the MethodInfo?
is there a simplier way to do it ?

i tried :

ParameterExpression dataRow = Expression.Parameter(typeof(DataRowExtensions), “dataRow”);
Expression left = Expression.Call(dataRow, typeof(DataRowExtensions).GetMethod(“Field”));

but it doesn’t work.


i want to create dynamic filters on the data inside IQueryable tempResults.

The user will check on checkboxes on the GUI that will add ‘Where’ expressions to the data in tempResults. when the user chooses “Column” i want to present the DataRows where ColumnName = “Column”.

that is why i need to create the where expression. but i’m so stuck on the MethodInfo thing. I tried this too:

MethodInfo FieldStringMethodInfo = typeof(DataRowExtensions).GetMethod("Field", BindingFlags.Public | BindingFlags.Static);

but it doesn’t work too.

Is there other ways to do it ?

  • 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-12T09:28:36+00:00Added an answer on May 12, 2026 at 9:28 am

    Replacement answer following clarification in comments:

    For successively building additional filters, you don’t need expression trees; you can call .Where multiple times (as necessary, once per search term) – for example:

    IEnumerable<DataRow> query = tempResults.AsEnumerable();
    if(!string.IsNullOrEmpty(value1)) {
        query = query.Where(row => row.Field<string>("Col1") == value1);
    }
    if (!string.IsNullOrEmpty(value2)) {
        query = query.Where(row => row.Field<string>("Col2") == value2);
    }
    

    The only thing to watch is the “capture” issue; be sure not to re-use any of the value1, value2 etc – otherwise the last value will apply to earlier filters…


    For an example of delegate combination (from comments) – note that I’ve dropped the DataTable aspect here purely to make the example shorter (it will work identically):

    public static class Predicate {
        public static Func<T, bool> OrElse<T>(
                this Func<T, bool> lhs, Func<T, bool> rhs) {
            return lhs == null ? rhs : obj => lhs(obj) || rhs(obj);
        }
        public static Func<T, bool> AndAlso<T>(
                this Func<T, bool> lhs, Func<T, bool> rhs) {
            return lhs == null ? rhs : obj => lhs(obj) && rhs(obj);
        }
    }
    class Data {
        public string Color { get; set; }
    }
    class Program {
        static void Main() {
            bool redChecked = true, greenChecked = true; // from UI...
            List<Data> list = new List<Data>() {
                new Data { Color = "red"},
                new Data { Color = "blue"},
                new Data { Color = "green"},
            };
            Func<Data, bool> filter = null;
            if (redChecked) {
                filter = filter.OrElse(row => row.Color == "red");
            }
            if (greenChecked) {
                filter = filter.OrElse(row => row.Color == "green");
            }
            if (filter == null) filter = x => true; // wildcard
    
            var qry = list.Where(filter);
    
            foreach (var row in qry) {
                Console.WriteLine(row.Color);
            }
        }
    }
    

    (original answer)

    Actually, that variant of LINQ won’t use an expression tree… it will use a delegate; but you can build the tree and compile it if you really want… I’m not sure why you would, though. What do you want to do? I’ll knock up an example…


    Here you go; this uses an expression tree, but I can’t think of a single good reason to do this, other than to prove that you can!

    public static class MyExtensions
    {
        public static IQueryable<TRow> Where<TRow, TValue>(
            this IQueryable<TRow> rows,
            string columnName, TValue value)
            where TRow : DataRow
        {
            var param = Expression.Parameter(typeof(TRow), "row");
            var fieldMethod = (from method in typeof(DataRowExtensions).GetMethods()
                               where method.Name == "Field"
                               && method.IsGenericMethod
                               let args = method.GetParameters()
                               where args.Length == 2
                               && args[1].ParameterType == typeof(string)
                               select method)
                               .Single()
                               .MakeGenericMethod(typeof(TValue));
            var body = Expression.Equal(
                Expression.Call(null,fieldMethod,
                    param,
                    Expression.Constant(columnName, typeof(string))),
                Expression.Constant(value, typeof(TValue))
            );
            var lambda = Expression.Lambda<Func<TRow, bool>>(body, param);
            return rows.Where(lambda);
    
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            DataTable tempResults = new DataTable();
            tempResults.Columns.Add("ColumnName");
            tempResults.Rows.Add("foo");
            tempResults.Rows.Add("Column");
    
            var test = tempResults.AsEnumerable().AsQueryable()
                       .Where("ColumnName", "Column");
            Console.WriteLine(test.Count());
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 242k
  • Answers 242k
  • 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 Someone will probably crucify me for the ugliness of this… May 13, 2026 at 7:38 am
  • Editorial Team
    Editorial Team added an answer You didn't remove EOL marker from lines you'lve read from… May 13, 2026 at 7:38 am
  • Editorial Team
    Editorial Team added an answer C# 4.0 in a Nutshell hasn't been released yet, but… May 13, 2026 at 7:38 am

Related Questions

Using LINQ to Entities, how can I determine if any item from a List
My application has to deal with large amounts of data, usual select size is
How would you create a regular expression that would parse the following url http://example.com/answers/38-my-example-question-on-regular-expression
Here's the problem I'm trying to solve. I want to pass a date, then

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.