简体   繁体   English

怎么转换DateTime? 直接转换为字符串(在C#linq中)

[英]How to convert DateTime? to String directly (in c# linq)

i'm making a web-service where i'm calling data-objects who are created directly through linq, i was wondering, how i can convert DateTime? 我正在做一个Web服务,我要调用直接通过linq创建的数据对象,我想知道如何转换DateTime? directly to string?, this, because i can't convert the DateTime? 直接转换为字符串?,这是因为我无法转换DateTime? to DateTime and then to string through a variable or a what not, because linq don't let me. 到DateTime,然后通过变量或其他方式进行字符串传递,因为linq不允许我这样做。

Here's the code: 这是代码:

lista = (from p in db.Acciones
     select new ItemAcciones
     {
        ID_Accion = p.ID_Accion,

        FechaHora =  p.FechaHora != null ? (DateTime)p.FechaHora : DateTime.Now,
        //ShowFechaHora = !String.IsNullOrWhiteSpace(((DateTime)p.FechaHora).ToString("HH:mm")) ? ((DateTime)p.FechaHora).ToString("HH:mm") : DateTime.Now.ToString("HH:mm"),

        ID_EmpresaNombre = p.Empresas.EmpresaNombre,
        ID_ResponsableNombre = p.Responsables.LoginID,
        ID_ContactoNombre = p.Contactos.Nombres + " " + p.Contactos.Paterno + " " + p.Contactos.Materno,
        ID_AccionTipoNombre = p.AccionTipos.AccionTipoGlosa,
        ID_AccionEstadoNombre = p.AccionEstados.AccEstGlosa,
        AccionGlosa = p.AccionGlosa, 
        ID_Empresa = p.ID_Empresa,
        AccionDescripcion = p.AccionDescripcion,
        ID_Negocio = (int?)p.ID_Negocio ?? 0,
        ID_Contacto = p.ID_Contacto,
        ID_AccionTipo = p.ID_AccionTipo,
        ID_AccionEstado = p.ID_AccionEstado,

        ID_ProgFecha = p.ID_ProgFecha != null ? (int)p.ID_ProgFecha : 0,
        ShowID_ProgFecha = String.IsNullOrWhiteSpace(p.ID_ProgFecha.ToString()) ?? p.ID_ProgFecha.ToString() : "--/--/----",
        //ShowID_ProgFecha = StringAEstilizarComoDate(Convert.ToString(p.ID_ProgFecha)),

        ID_ProgHora = p.ID_ProgHora != null ? (int)p.ID_ProgHora : 0,
        //ShowID_ProgHora = StringAEstilizarComoHora(Convert.ToString(p.ID_ProgHora)),

        ID_EjecFecha = p.ID_EjecFecha != null ? (int)p.ID_EjecFecha : 0,
        //ShowID_EjecFecha = StringAEstilizarComoDate(Convert.ToString(p.ID_EjecFecha)),

        ID_EjecHora = p.ID_EjecHora != null ? (int)p.ID_EjecHora : 0,
        //ShowID_EjecHora = StringAEstilizarComoHora(Convert.ToString(p.ID_EjecHora)),

        ID_Responsable = p.ID_Responsable,
        EmpresaRUTCompleto = p.Empresas.EmpresaRut.ToString() + "-" + p.Empresas.EmpresaDV,
        ID_NegocioNombre = p.Negocios.ProyectoNombre,
     }).ToList();

Any question, suggestion or comment to improve the question would be appreciated as well as the answer. 任何问题,建议或评论,以改善该问题以及答案。

It's a manually solution, but you can use SqlFunctions to handle the string for your Date. 这是一个手动解决方案,但是您可以使用SqlFunctions处理Date的字符串。

var qqqq = db.YourTable.Select(q => (q.YourDate == null ? "" : (SqlFunctions.DateName("day",q.YourDate) + "/" + SqlFunctions.DateName("month", q.YourDate) + "/" + SqlFunctions.DateName("year", q.YourDate) + " " + SqlFunctions.DateName("hour", q.YourDate) + " " + SqlFunctions.DateName("minute", q.YourDate)))).ToList();

Hope that helps. 希望能有所帮助。 =) =)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM