Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm trying to convert Nullable Datetime to string in my Linq Query. And I get this error
LINQ to Entities does not recognize the method 'System.Nullable`1[System.Int32] DatePart(System.String, System.Nullable`1[System.DateTime])' method, and this method cannot be translated into a store expression.


Please help me

[EDIT - OP's code from comment]
C#
from p in TreatmentDetails
    select new
    {
        p.Id,
        p.TotalQty,
        p.Dose,
        p.IsStop,
        p.Note,
        p.TandFId,
        PrescriptionDate = SqlFunctions.DateName("day", p.PrescriptionDate).Trim() + "/" +
        SqlFunctions.StringConvert((double)p.PrescriptionDate.Value.Month).TrimStart() + "/" +
        SqlFunctions.DateName("year", p.PrescriptionDate)
                                    }).ToList();
Posted
Updated 18-Jan-14 3:55am
v3
Comments
Karthik_Mahalingam 18-Jan-14 3:48am    
post your code..
Tejas Vaishnav 18-Jan-14 6:03am    
can you share your linq code...
arahaman.ashik 18-Jan-14 9:47am    
from p in TreatmentDetails
select new
{
p.Id,
p.TotalQty,
p.Dose,
p.IsStop,
p.Note,
p.TandFId,
PrescriptionDate = SqlFunctions.DateName("day", p.PrescriptionDate).Trim() + "/" +
SqlFunctions.StringConvert((double)p.PrescriptionDate.Value.Month).TrimStart() + "/" +
SqlFunctions.DateName("year", p.PrescriptionDate)
}).ToList();
Ganesh KP 18-Jun-14 2:14am    
Thanks for your answer. It helps me.

Your error seems to be generated from your next code section:
SqlFunctions.StringConvert((double)p.PrescriptionDate.Value.Month).TrimStart(); 

and especially the usage of:
p.PrescriptionDate.Value.Month

So you should modify this code section by using:
DateName("month", p.PrescriptionDate).Trim()
 
Share this answer
 
v4
Comments
arahaman.ashik 19-Jan-14 10:39am    
Thanks for your answers, but I tried this,
DateName("month", p.PrescriptionDate).Trim()

and get the same error...
Raul Iloc 20-Jan-14 4:23am    
In this case you should modify your code to manage the case when your DateTime is null.
You should do change in the next manner:
select new{ ...
PrescriptionDate == null ? " " : "[your old code]"
}).ToList();

so in the case of DateTime null I put string space but you could put the string that you want.
LINQ Does not allow you to do any type of converting
you can do your converting out of linq code
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900