Giter VIP home page Giter VIP logo

Comments (1)

AlexanderKrutov avatar AlexanderKrutov commented on May 28, 2024

@VictorioBerra this is because of different formatting of dates used on server and client side. In the example above, there are 2 formats:

  • JavaScript: MMMM dd, yyyy
  • SQL Server Compact: MMM d yyyy hh:mmtt

When DataTables.Queryable builds an expression tree, by default it uses ToString() method for any non-string property of model. This C# expression:

.Where(p => p.StartDate.ToString().Contans("Mar"))

is translated to following SQL query:

...
WHERE ( CAST( [Extent1].[StartDate] AS nvarchar(4000)) IS NOT NULL) AND ( CAST( [Extent1].[StartDate] AS nvarchar(4000)) LIKE N'%Mar%')
...

Casting to nvarchar causes date to be formatted like: Mar 9 2011 12:00AM.
That's why searching for "Mar" works but "March" does not. You should use custom search predicate to tell DataTables.Queryable to use correct format.
Because EntityFramework is used, some limitations are exist. For example, you can not use StartDate.ToString("MMMM dd, yyyy") because it can not be translated to SQL query correctly.

Instead you should write:

var request = new DataTablesRequest<Person>(Request.QueryString);
var dateColumn = request.Columns[p => p.StartDate];
var searchValue = dateColumn.SearchValue;

// Force searching by StartDate with format "MMMM dd, yyyy"
dateColumn.ColumnSearchPredicate = p => (
    SqlCeFunctions.DateName("month", p.StartDate) + " " + 
    SqlCeFunctions.Replicate("0", 2 - SqlCeFunctions.DateName("dd", p.StartDate).Length) + 
    SqlCeFunctions.DateName("dd", p.StartDate) + ", " + 
    SqlCeFunctions.DateName("yyyy", p.StartDate)).Contains(searchValue);

using (var ctx = new DatabaseContext())
{
    var persons = ctx.Persons.Include("Office.Address").ToPagedList(request);
    return JsonDataTable(persons);
}

Of course if DataTables.Queryable is used with in-memory data, you can simply write:

dateColumn.ColumnSearchPredicate = p => p.StartDate.ToString("MMMM dd, yyyy").Contains(searchValue);

from datatables.queryable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.