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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:48:13+00:00 2026-05-16T17:48:13+00:00

I am attempting to create a dynamic query using expression trees in LINQ to

  • 0

I am attempting to create a dynamic query using expression trees in LINQ to represent the following query

WageConstIns.Where(WageConstIn => WageConstIn.Serialno.ToString().StartsWith("2800"));

I have attempted to create it like so:

MemberExpression le1 = LinqExpression.Property(paramExp, "Serialno");
MethodCallExpression le2 = LinqExpression.Call(le1, typeof(string).GetMethod("ToString",  System.Type.EmptyTypes));
ConstantExpression le3 = LinqExpression.Constant("2800");
MethodCallExpression le4 = LinqExpression.Call(le2, typeof(string).GetMethod("StartsWith"));

I am getting an error during runtime. How can the above query best be built using expression trees?

  • 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-16T17:48:14+00:00Added an answer on May 16, 2026 at 5:48 pm

    The easiest way would be to just declare it as an Expression<Func<...>>

    public static class Program {
        public static void Main() {
            Expression<Func<DummyClass, Boolean>> predicate = WageConstIn => WageConstIn.Serialno.ToString().StartsWith("2800");
        }
    }
    

    But if you want to construct it using different Expressions…

    public static class Program {
        public static void Main() {
            var param = Expression.Parameter(typeof(DummyClass), "WageConstIn");
            var constValue = Expression.Constant("2800");
    
            // WageConstIn => WageConstIn.Serialno.ToString().StartsWith(...)
            var first = Expression.Lambda(
                parameters: param,
                body: Expression.Call(
                    instance: Expression.Call(
                        instance: Expression.Property(param, "Serialno"),
                        methodName: "ToString",
                        typeArguments: null,
                        arguments: null
                    ),
                    methodName: "StartsWith",
                    typeArguments: null,
                    arguments: new[] { constValue }
                )
            );
    
            // WageConstIn => Convert.ToString(WageConstIn.Serialno).StartsWith(...)
            var second = Expression.Lambda(
                parameters: param,
                body: Expression.Call(
                    instance: Expression.Call(
                        type: typeof(Convert),
                        methodName: "ToString",
                        typeArguments: null,
                        arguments: new[] { Expression.Property(param, "Serialno") }
                    ),
                    methodName: "StartsWith",
                    typeArguments: null,
                    arguments: new[] { constValue }
                )
            );
        }
    }
    

    Most people [that I’ve talked to] who enter the domain of expression trees are usually satisfied with the System.Linq.Dynamic functionality. (Which can be abused into a lot of different ways.) This code snippet of pure awesomeness is a part of the Visual Studio sample code, probably hiding somewhere on your harddrive already.

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

Sidebar

Related Questions

I'm attempting to create a table for monitoring purposes using the following script: $w3wppriv
I'm attempting to create a dynamic map using the latest Google Map API. Everything
I'm attempting to use JQuery to create a dynamic page header using an image
I attempting to create custom tabs using this . But when I try to
I'm attempting to create a T-SQL case statement to filter a query based on
As I was attempting to create a Dynamic Web Project in eclipse I received
I'm trying to create a generic function to build a Linq expression from a
I am attempting to create a dynamic list of files available in an ftp
I'm attempting to create an xquery expression that will return selected nodes but will
I'm attempting to create a user login for Facebook and Windows LiveId using DotNetOpenAuth

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.