在Linq中将Datetime转换为String的问题 [英] Problem with Converting Datetime to String in Linq

查看:183
本文介绍了在Linq中将Datetime转换为String的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试将Nullable Datetime转换为我的Linq查询中的字符串。我收到此错误

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


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();

推荐答案

您的错误似乎是从您的下一个代码部分生成的:

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()


LINQ不允许你进行任何类型的转换

你可以转换出linq代码
LINQ Does not allow you to do any type of converting
you can do your converting out of linq code


这篇关于在Linq中将Datetime转换为String的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆