Linq to SQL Format Date time in One Take

2019-08-06 14:40发布

问题:

This was asked many times on stackoverflow on how to format the date time in a linq to sql expression, and the solution was always to force a compilation of the linq first using ToList() and then process the datetime using the luxury of the .ToString("hh:mm") formatting options.

But I'm trying to do it in one take and I partially succeeded, except that the code is both terrible and ugly, any shorter way to do this, assuming that the date object is a unix timestamp stored in the db, im trying to return just the timepart as 4:54 pm.

            production_cycles = from p in db.ProductionCycles
                                where p.IsRunning == true
                                select new Rest.ProductionCycle {
                                    id = p.ID,
                                    name = p.Name,
                                    created = p.Created,
                                    steps = from s in p.Logs
                                            where user_permissions.Contains(s.Permission.ID)
                                            orderby s.ID ascending
                                            select new Rest.Step {
                                                created = s.Created,
                                                created_label = DbFunctions.CreateDateTime(
                           (int)SqlFunctions.DatePart("yyyy", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))),
                           (int)SqlFunctions.DatePart("m", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))),
                           (int)SqlFunctions.DatePart("d", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))),
                           (int)SqlFunctions.DatePart("hh", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))),
                           (int)SqlFunctions.DatePart("mi", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))),
                           0

                          ).Value.Hour.ToString() 
                          + ":" +
                            DbFunctions.CreateDateTime(
                           (int)SqlFunctions.DatePart("yyyy", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))),
                           (int)SqlFunctions.DatePart("m", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))),
                           (int)SqlFunctions.DatePart("d", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))),
                           (int)SqlFunctions.DatePart("hh", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))),
                           (int)SqlFunctions.DatePart("mi", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))),
                           0

                          ).Value.Minute.ToString()                         


                          ,
                                                username = s.User.Name,

回答1:

Please try this out :

var baseDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
production_cycles = from p in db.ProductionCycles
    where p.IsRunning == true
    select new Rest.ProductionCycle
    {
        id = p.ID,
        name = p.Name,
        created = p.Created,
        steps = from s in p.Logs
            where user_permissions.Contains(s.Permission.ID)
            orderby s.ID ascending
            select new Rest.Step
            {
                created = s.Created,
                created_label = DbFunctions.DiffHours(baseDate, s.Created).ToString + ":" + DbFunctions.DiffMinutes(baseDate, s.Created).ToString + ":" +DbFunctions.DiffSeconds(baseDate, s.Created).ToString

                ////// the rest as usual

            },
    };