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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:55:55+00:00 2026-05-24T22:55:55+00:00

I am trying to convert a DateTime to a String before calling contains on

  • 0

I am trying to convert a DateTime to a String before calling contains on it. However despite my efforts of putting the result of one expression into another I fail miserably.

The code is derived from the highest answer to this question jqgrid with asp.net webmethod and json working with sorting, paging, searching and LINQ — but needs dynamic operators.

Assume I have the following method as StringExtension from the question:

public static class StringExtensions
{
  public static MemberExpression ToMemberExpression(this string source, ParameterExpression p)
  {
    if (p == null)
        throw new ArgumentNullException("p");

    string[] properties = source.Split('.');

    Expression expression = p;
    Type type = p.Type;

    foreach (var prop in properties)
    {
        var property = type.GetProperty(prop);
        if (property == null)
            throw new ArgumentException("Invalid expression", "source");

        expression = Expression.MakeMemberAccess(expression, property);
        type = property.PropertyType;
    }

    return (MemberExpression)expression;
  }
}

Therefore I have the following method also from the question which I have then adapted for DateTime.

public virtual Expression<Func<T, bool>> CreateExpression<T>(string searchField, string searchString, string searchOper)
    {
        Expression exp = null;
        var p = Expression.Parameter(typeof(T), "p");

        Expression propertyAccess = searchField.ToMemberExpression(p);

        switch (searchOper)
        {
            case "bw":
                exp = Expression.Call(propertyAccess, typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) }), Expression.Constant(searchString));
                break;

            // New code by me
            case "cn":

                if (propertyAccess.Type == typeof(DateTime))
                {
                    // My faulty logic - from Jon Skeet answer below

                    Expression toStringCall = Expression.Call(
                        propertyAccess, "ToString",
                        null,
                        new[] { Expression.Constant("D") });

                    Expression containsCall = Expression.Call(
                        toStringCall, "Contains",
                        null,
                        new[] { Expression.Constant(searchString) });

                    exp = containsCall;
                }
                else
                {
                    // Unchanged
                    exp = Expression.Call(propertyAccess, typeof(string).GetMethod("Contains", new Type[] { typeof(string) }), Expression.Constant(searchString));
                }
                break;
            case "ew":
                exp = Expression.Call(propertyAccess, typeof(string).GetMethod("EndsWith", new Type[] { typeof(string) }), Expression.Constant(searchString));
                break;
            case "gt":
                exp = Expression.GreaterThan(propertyAccess, Expression.Constant(searchString, propertyAccess.Type));
                break;
            case "ge":
                exp = Expression.GreaterThanOrEqual(propertyAccess, Expression.Constant(searchString, propertyAccess.Type));
                break;
            case "lt":
                exp = Expression.LessThan(propertyAccess, Expression.Constant(searchString, propertyAccess.Type));
                break;
            case "le":
                exp = Expression.LessThanOrEqual(propertyAccess, Expression.Constant(searchString, propertyAccess.Type));
                break;
            case "eq":
                exp = Expression.Equal(propertyAccess, Expression.Constant(searchString.ToType(propertyAccess.Type), propertyAccess.Type));
                break;
            case "ne":
                exp = Expression.NotEqual(propertyAccess, Expression.Constant(searchString, propertyAccess.Type));
                break;
            default:
                return null;
        }

        return (Expression<Func<T, bool>>)Expression.Lambda(exp, p);
    }

I get the following exception.

LINQ to Entities does not recognize the method ‘System.String ToString(System.String)’ method, and this method cannot be translated into a store expression.

  • 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-24T22:55:56+00:00Added an answer on May 24, 2026 at 10:55 pm

    I suspect you want something like this (fixed from previous version):

    using System;
    using System.Linq.Expressions;
    
    public class Person
    {
        public DateTime DateOfBirth { get; set; }
    }
    
    public class Test
    {
        static void Main()
        {
            var expr = Foo<Person>("DateOfBirth", "1976");
    
            Person p = new Person
            {
                DateOfBirth = new DateTime(1976, 6, 19)
            };
    
            Console.WriteLine(expr.Compile()(p));
        }
    
        static Expression<Func<T, bool>> Foo<T>(string propertyName,
                                                string searchValue)
        {
            ParameterExpression parameter = Expression.Parameter(typeof(T), "x");
            Expression property = Expression.Property(parameter, propertyName);
            Expression toStringCall = Expression.Call(
                property, "ToString",
                null,
                new[] { Expression.Constant("D") });
    
            Expression containsCall = Expression.Call(
                toStringCall, "Contains",
                null,
                new[] { Expression.Constant(searchValue) });
    
            return Expression.Lambda<Func<T, bool>>(containsCall, parameter);
        }
    }
    

    Note that the “null” values are to show that it’s a non-generic method call.

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

Sidebar

Related Questions

how do i convert the following datetime string that contains a timezone designation into
I am trying to convert a string into datetime with the following C# code,
I am trying to convert a string into date time format. DateTime.Parse(tempfrmBankDetails.dgvBankDetails.SelectedRows[0].Cells[PaymentDate].Value.ToString(),null); This is
How can I convert a string 08:00 to a DateTime datatype? Im trying to
Possible Duplicate: Convert string to DateTime in c# I am trying to convert a
In the formula below I'm trying to convert a custom date string (yyyymmddhhmmss) into
I'm trying to convert a string into a date format My string looks like
I'm trying to convert a string 20091229050936 into 05:09 29 December 2009 (UTC) >>>import
I'm trying to convert a string from to a Joda DateTime object. The date
I am trying to convert all DateTime values in a DataTable to strings. Here

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.