Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How can I format a datetime (nullable) field value in select? #51

Closed
winsonet opened this issue Nov 1, 2016 · 7 comments
Closed
Assignees

Comments

@winsonet
Copy link

winsonet commented Nov 1, 2016

I want to format a nullable datetime value, how can I do that?

For example in Linq:

var expected1 = _context.Blogs.Select(x =>
    (string)((DateTime)(x.ModifyDate ?? x.ModifyDate.Value)).ToString("yyyy-MM-dd")).ToArray();

and I tried in dynamic Linq:

var test1 = _context.Blogs.Select<string>(
    "(string)((datetime)(ModifyDate ?? ModifyDate.Value)).ToString(\"yyyy-MM-dd\")").ToArray();

but I got the below error:

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

any ideas?

thanks!!

@jotab123
Copy link
Contributor

jotab123 commented Nov 10, 2016

I don't know if it's possible to do in Select but you could do:

var test1 = _context.Blogs.AsQueryable().Where("ModifyDate != null")
.Select("ModifyDate.Value.ToString(\"yyyy-MM-dd\")");

@winsonet
Copy link
Author

jotab123, thanks for your reply, but this is not my purpose, in your way, just find the not null value, but actually, I need to find all values, if it's null then will show empty or other thing, otherwise will format it and display

@StefH
Copy link
Collaborator

StefH commented Nov 12, 2016

Why not just execute the ToString method on a list instead of the queryable ? In that case you don't have issues with Linq-SQL.

@winsonet
Copy link
Author

sorry, can you give me a sample how to do that? thanks!

@StefH
Copy link
Collaborator

StefH commented Nov 12, 2016

var list = _context.Blogs.Select("it.ModifyDate").ToDynamicList<DateTime>();
var result = list.Select(x => x ?? x.Value)).ToString("yyyy-MM-dd")).ToList();

@winsonet
Copy link
Author

winsonet commented Nov 12, 2016

but for my case, I need to generate a full dynamic query for get the result, and I don't want to convert to list at first, because I have a lot of data in the table, if it to list first then will get all data and the performance will be bad, that why I need to use IQueryable .

and I know if I convert to list object then I can easy to use HasValue method in the query.

@StefH StefH changed the title How can I format a datetime (nullable) field value in select? [Question] How can I format a datetime (nullable) field value in select? Jun 9, 2017
@StefH
Copy link
Collaborator

StefH commented Jun 9, 2017

Did you already try the ? operator ?

Like

var q = _context.Blogs.AsQueryable();
var x = q.Select("it.ModifyDate != null ? [[[format to string]]] : null");

@StefH StefH closed this as completed Oct 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants