Comments (1)
@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)
- Unused code?
- Better error handling when the request tries to reflect to the desired type.
- Ordering on a complex type does not work. HOT 3
- .net core 2.0 JsonDataTable ? HOT 3
- Using the library with in-memory objects HOT 5
- Support passing up CancellationTokens to ToPagedListAsync(). HOT 4
- AutoMapper ProjectTo() blows up. HOT 4
- ToNameValueCollection() throws when a column is provided but a search is not. HOT 2
- Multi column ordering does not work? HOT 2
- NullReferenceException inside GetPropertyByName HOT 2
- Support for Multiple Filter Values on Same Column HOT 2
- NullReferenceException inside BuildStringContainsPredicate HOT 6
- I really need to know what name/data failed when calling GetPropertyByName. HOT 1
- I think `name` should try to be parsed BEFORE `data`. Here is why: HOT 3
- Add custom Data Fields to DataTablesAjaxPostModel HOT 2
- How to add (Edit &Delete) buttons in datatable? HOT 1
- Can we use/call SQL store procedure? HOT 1
- How to use with Razor Pages HOT 2
- ToPagedListAsync Call is ambiguous HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from datatables.queryable.