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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:32:04+00:00 2026-05-30T00:32:04+00:00

I have been banging my head on this problem for sometime. There are some

  • 0

I have been banging my head on this problem for sometime. There are some similar cases, but the solutions weren’t applicable on my case.

I have a method that returns filter query in string format. The method has logic for different data types, sets correct values, column names etc.

string filterQuery = GetFilterQuery(params);
rows = rows.Where(filterQuery);

My problem is that I have Nullable DateTime in the database and I have String representation in the code side.

I have tried following queries (String representation might be wrong currently):

"BirthDate.ToString() = \"16.2.2012 22:00:00\""

Result: Methods on type ‘DateTime?’ are not accessible

"BirthDate.Value.ToString() = \"16.2.2012 22:00:00\""

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

"BirthDate == null ? 1=1 : (DateTime)BirthDate.ToString() = \"16.2.2012 22:00:00\""

Result: ‘.’ or ‘(‘ expected

Any ideas how to solve the problem?

Update (more source code added about query generation)

var filterQueries = query.GridFilteringOptions.filters
    // remove filters that doesn't have all the required information
    .Where(o => o.name != string.Empty && o.value != string.Empty && !string.IsNullOrEmpty(o.type))
    // remove filters that are filtering other tables than current
    .Where(o => o.table == tableName) 
    .Select(filter => filter.ResolveQuery()).ToList();

if (filterQuery.Any())
{
    var filterQuery = string.Join(" And ", filterQueries);
    rows = rows.Where(filterQuery);
}

And here is a class Filter and methods are related to this context

public string ResolveQuery()
{
    if (type == "Int64")
    {
        return ResolveInteger();
    }
    else if(type == "String")
    {
        return ResolveString();
    }
    else if(type == "DateTime")
    {
        return ResolveDateTime();
    }
    else
    {
        return string.Empty;
    }
}

private string ResolveDateTime()
{
    DateTime result = new DateTime();
    if (DateTime.TryParse(this.value, out result))
    {
        return string.Format("{0}.ToString() = \"{1}\"", this.name, result.ToUniversalTime());
    }
    return string.Empty;
}

private string ResolveString()
{
    switch (@operator)
    {
        default:
            return string.Format(@"{0}.StartsWith(""{1}"")", this.name, this.value);
    }            
}

private string ResolveInteger()
{
    string tmp = this.name;
    switch (@operator)
    {
        case -1:
            return string.Empty;
        case 0:
            tmp += "<";
            break;
        case 1:
            tmp += "=";
            break;
        case 2:
            tmp += ">";
            break;
        default:
            return string.Empty;
    }
    tmp += value;
    return tmp;
}
  • 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-30T00:32:05+00:00Added an answer on May 30, 2026 at 12:32 am

    LINQ to Entities doesn’t recognize the ToString() method. You would have to evaluate it before inserting the resulting string into your query.

    To create the examples in your question you might handle it like this:

    // "BirthDate.ToString() = \"16.2.2012 22:00:00\""
    string birthdate = BirthDate.ToString();
    string query = String.Format("{0}  = \"16.2.2012 22:00:00\"", birthdate);
    
    // "BirthDate.Value.ToString() = \"16.2.2012 22:00:00\""
    string birthdate = BirthDate.Value.ToString();
    string query = String.Format("{0}  = \"16.2.2012 22:00:00\"", birthdate);
    

    "BirthDate == null ? 1=1 : (DateTime)BirthDate.ToString() = \"16.2.2012 22:00:00\"" probably doesn’t work because LINQ to EF doesn’t recognize the ternary operator ( ? : )

    Edit: I understand from your comment that BirthDate is a column in your table, not a variable. In this case you can retrieve all entries, convert them to a list and then apply the filter using LINQ to Objects like this (although you would have to modify your filterQuery accordingly):

    string filterQuery = GetFilterQuery(params);
    var filteredRows = rows.ToList().Where(filterQuery);
    

    Untested: It might be possible to use your database’s CONVERT function:

    string query = "CONVERT(varchar(20), BirthDate) = \"16.2.2012 22:00:00\"";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Been banging my head on this for a while now The problem I have
Been banging my head against this problem all morning. I have setup a connection
I have been banging my head against this for some time now: I want
I have been banging my head against this problem for days, and searched exhaustively
I have been banging my head on this one all day. The C++ project
I have been banging my head against the wall with this odd behaviour on
I have been banging my head on a wall with this one. I need
This is to all the C# gurus. I have been banging my head on
all. I've been banging my head for a few hours because of this problem.
I have been banging my head on this (im a newbie in MySQL, be

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.