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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:33:55+00:00 2026-06-15T01:33:55+00:00

I have been learning Expressions and using the code below to add build up

  • 0

I have been learning Expressions and using the code below to add build up an expression against a database model (EF4 – ORACLE not SQL!)

This works perfectly against Oracle, and allows me to dynamically build up predicates such as "CustomerId", "Contains", 2 into f=>f.CustomerId.ToString().ToLower().Contains("2")

However, if I try against SQL Server then it fails because I need to call SqlFunctions.StringConvert – but I don’t know how to get that included in the Lambda?

My end result would be something like:

f=> SqlFunctions.StringConvert(f.CustomerId).ToLower().Contains("2")

Thx 🙂


EDIT: Added example of what I have tried

This code looks like it almost works, sort of!
However, it throws an error on the var sqlExpression line

Expression of type 'System.Double' cannot be used for parameter of type 'System.Nullable`1[System.Double]' of method 'System.String StringConvert(System.Nullable`1[System.Double])'

MethodInfo convertDouble = typeof(Convert).GetMethod("ToDouble",new Type[]{typeof(int)});
                    var cExp = Expression.Call(convertDouble, left.Body);

                    var entityParam = Expression.Parameter(typeof(TModel), "f");
                    MethodInfo sqlFunc = typeof(SqlFunctions).GetMethod("StringConvert", new Type[] { typeof(double) });
                    var sqlExpression = Expression.Call(sqlFunc, cExp);


                    MethodInfo contains = typeof(string).GetMethod("Contains", new[] { typeof(string) });
                    right = Expression.Constant(value.ToString(), typeof(string));

                    var result = left.AddToString().AddToLower().AddContains(value.ToString());
                    return result;

public static Expression<Func<T, string>> AddToString<T, U>(this Expression<Func<T, U>> expression)
        {

            return Expression.Lambda<Func<T, string>>(
                Expression.Call(expression.Body,
                "ToString",
                null,
                null),
                expression.Parameters);
        }

        public static Expression<Func<T, string>> AddToLower<T>(this Expression<Func<T, string>> expression)
        {

            return Expression.Lambda<Func<T, string>>(
                Expression.Call(expression.Body,
                "ToLower",
                null,
                null),
                expression.Parameters);
        }

        public static Expression<Func<T, bool>> AddContains<T>(this Expression<Func<T, string>> expression, string searchValue)
        {
            return Expression.Lambda<Func<T, bool>>(
                Expression.Call(
                    expression.Body,
                    "Contains",
                    null,
                    Expression.Constant(searchValue)),
                expression.Parameters);
        }
  • 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-15T01:33:56+00:00Added an answer on June 15, 2026 at 1:33 am

    I believe you basically need to build an equivalent expression of the following lambda expression:

    e => SqlFunctions.StringConvert((double?) e.Number).Contains("6"))
    

    Here is a full copy-paste example. It uses CodeFirst so should work without having to create database or anything like that. Just add the Entity Framework nuget package (I used EF6 but it should work for EF5 as well). Build lambda is what you are really after.

    namespace ConsoleApplication8
    {
        public class MyEntity
        {
            public int Id { get; set; }
            public int Number { get; set; }
        }
    
    
        public class MyContext : DbContext
        {
            public DbSet<MyEntity> Entities { get; set; }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                using (var ctx = new MyContext())
                {
                    if (!ctx.Entities.Any())
                    {
                        ctx.Entities.Add(new MyEntity() {Number = 123});
                        ctx.Entities.Add(new MyEntity() {Number = 1893});
                        ctx.Entities.Add(new MyEntity() {Number = 46});
                        ctx.SaveChanges();
                    }
    
                    foreach(var entity in ctx.Entities.Where(e => SqlFunctions.StringConvert((double?) e.Number).Contains("6")))
                    {
                        Console.WriteLine("{0} {1}", entity.Id, entity.Number);
                    }
    
                    foreach (var entity in ctx.Entities.Where(BuildLambda<MyEntity>("Number", "6")))
                    {
                        Console.WriteLine("{0} {1}", entity.Id, entity.Number);
                    }
    
                }
            }
    
            private static Expression<Func<T, bool>> BuildLambda<T>(string propertyName, string value)
            {
                var parameterExpression = Expression.Parameter(typeof(T), "e");
    
                var stringConvertMethodInfo = 
                    typeof(SqlFunctions).GetMethod("StringConvert", new Type[] {typeof (double?)});
    
                var stringContainsMethodInfo =
                    typeof (String).GetMethod("Contains");
    
                return 
                    Expression.Lambda<Func<T, bool>>(
                    Expression.Call(
                        Expression.Call(
                            stringConvertMethodInfo,
                            Expression.Convert(
                                Expression.Property(parameterExpression, "Number"),
                                typeof (double?))),
                        stringContainsMethodInfo,
                        Expression.Constant(value)),
                    parameterExpression);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have been learning WPF with Expression Blend 4 last few days and
Have been learning about MVP and have tried writing a test app using it
I have been learning MVVM/EF4 (for C#) recently and have followed Julie Lerman's videos.
I have been using this regular expression to extract file names out of file
I have been learning to develop websites using ASP.NET MVC 2 for work... and
Have been learning ASP.NET (using C#) over the past few days. I have made
I have been learning on classes in c++ and I got a certain code
I have been learning how to create dynamic images and dynamic stylesheets using ASP.NET
I have been learning Tkinter, i have written a small code where i want
I have been learning prolog.. I'm using a editor named prol1.1.1 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.