You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a query like db.Records.Where(record => (record.Created.ToString("MM/dd/yyyy hh:mm:ss tt") ?? "").ToLower().Contains("11/")).ToList(); where Created is a DateTime
The query that is generated does not contain any where so in this example it doesn't appear to have an effect, but when I add other criteria that limits the result set then we start to see the issue with the query not executing in SQL.
My use case here is that I have a table rendered on the page and I am setting the display format of the DateTime. I allow the user to search that table column, but to provide them reliable results I need to know the format the user sees to know how to search it in the database. The result set is huge so rendering everything in the client and sorting client side is not an option.
Further technical details
EF Core version: 2.0.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017
@los93sol - It appears you want to allow your users to search the Created date for records created on specific days of the month, or months of the year?
Thanks for the suggestion, that is similar to what I’m trying to do, but I️ was hoping to allow the user to search it as a string to avoid building a picker for the UI
You could create a User defined function in your context for which you can provide a SQL translation and we would just invoke it on server. That would allow you to translate date to specific conversion format on server rest would just work.
Example (Put this in your derived DbContext class
protectedoverridevoidOnModelCreating(ModelBuildermodelBuilder){// Configure modelmodelBuilder.HasDbFunction(typeof(MyContext).GetMethod(nameof(MyContext.ToCustomString))).HasTranslation(
e =>{returnnewSqlFunctionExpression("convert",typeof(string),new[]{newSqlFragmentExpression("VARCHAR(100)"),e.First(),newSqlFragmentExpression("101"),// Pick a style available on T-SQL. });});}publicstaticstringToCustomString(DateTimedate){returndate.ToString("MM/dd/yyyy hh:mm:ss tt");// You can just throw NotImplementedException too}
Thank you! That's almost perfect and exactly what I was looking for, but I am in an abstraction layer where I am providing the functionality via an extension method of IQueryable and cannot figure out how to get at the modelBuilder. If I could sort that out then I think I know what to do from there to sort it on my end.
One restriction is method has to be static. You can move it to different type than DbContext if needed. To be able to call from IQueryable and OnModelCreating both you need to put in some place which is referenced by both.
@pmiddleton, thanks for pointing that out, my plan was to actually change this to use format instead of convert, thinking that would be more efficient in sql
Activity
smitpatel commentedon Nov 6, 2017
Related #7364
pmiddleton commentedon Nov 7, 2017
@los93sol - It appears you want to allow your users to search the Created date for records created on specific days of the month, or months of the year?
If that is the case then you can do the following
This will translate into DATEPART method calls.
los93sol commentedon Nov 7, 2017
Thanks for the suggestion, that is similar to what I’m trying to do, but I️ was hoping to allow the user to search it as a string to avoid building a picker for the UI
smitpatel commentedon Nov 7, 2017
You could create a User defined function in your context for which you can provide a SQL translation and we would just invoke it on server. That would allow you to translate date to specific conversion format on server rest would just work.
Example (Put this in your derived DbContext class
Query
Generates SQL
los93sol commentedon Nov 7, 2017
Thank you! That's almost perfect and exactly what I was looking for, but I am in an abstraction layer where I am providing the functionality via an extension method of IQueryable and cannot figure out how to get at the modelBuilder. If I could sort that out then I think I know what to do from there to sort it on my end.
smitpatel commentedon Nov 7, 2017
One restriction is method has to be static. You can move it to different type than DbContext if needed. To be able to call from IQueryable and OnModelCreating both you need to put in some place which is referenced by both.
pmiddleton commentedon Nov 7, 2017
One thing to consider is that you are going to have bad performance on with this query.
The query is going to force a conversion on your Created column, from datetime to nvarchar, which will prevent using any indexes in the query.
Depending on how big the table is this could be a problematic query.
los93sol commentedon Nov 7, 2017
@pmiddleton, thanks for pointing that out, my plan was to actually change this to use format instead of convert, thinking that would be more efficient in sql
ajcvickers commentedon Nov 8, 2017
Duplicate of #7364