2016-06-07 11 views
0

Ich mache einen Web-Service, wo ich Datenobjekte anrufe, die direkt über linq erstellt werden, ich frage mich, wie ich DateTime konvertieren kann? direkt zu string ?, dies, weil ich die DateTime nicht konvertieren kann? zu DateTime und dann durch eine Variable oder was nicht zu string, weil linq mich nicht lassen.Wie konvertiert man DateTime? direkt zu String (in C# linq)

Hier ist der 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(); 

Jeder mögliche Frage, Kommentar oder Verbesserungsvorschlag, die Frage zu verbessern würde und die Antwort geschätzt.

+2

BTW, können Sie schreiben 'p.ID_ProgFecha ?? 0 ' – SLaks

+0

Holen Sie einfach die' DateTime's, führen Sie die Umwandlung in berechneten Eigenschaften in 'ItemAcciones' durch. –

Antwort

-1

Es ist eine manuelle Lösung, aber Sie können SqlFunctions verwenden, um die Zeichenfolge für Ihr Date zu behandeln.

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

Hoffe, dass hilft. =)

+0

Schlechte Beratung. Dies löst ein verzögertes Laden für jedes verschachtelte Objekt aus ('p.Empresas' usw.) –

Verwandte Themen