0

I have troubles formatting two datetime (actually, it's just the time part) values in my LINQ query. The values for time I'm getting in my gridview (gvDaily) have the following format: "HH:mm:ss-HH:mm:ss". What I'd like to have is to have "HH:mm-HH:mm" time format, but I'm not sure how to accomplish it. Needless to mention that it's the Time part of the query that I have problems with.

var dailyList = (from d in db.Daily
                         select d).ToList();

gvDaily.DataSource = from d in dailyList
                     orderby d.Datum
                     select new { d.idDaily, d.Biljeske, d.Datum, d.EfektivnoSati, Tvrtka = d.Ticket.Firma.Naziv, DailyManager = d.Kontakt.Ime + " " + d.Kontakt.Prezime, Ticket = d.Ticket.Opis, Time= d.TimeFrom+ "-" + d.TimeTo, d.TimeFrom, d.TimeTo, d.Opis, d.Ticket.Zatvoren, d.Ticket.IzdanRacun, TicketNumber = d.Ticket.idTicket + "-" + d.Ticket.RedniBroj, d.Dolazak };

Thank you in advance!

P.S. Custom formatting doesn't work so I'm trying to find some other ideas.

xanatos
  • 109,618
  • 12
  • 197
  • 280
wegelagerer
  • 3,600
  • 11
  • 40
  • 60
  • Look for my answer, hope this will help you http://stackoverflow.com/questions/2428128/linq-2-sql-datetime-format-to-string-yyyy-mm-dd/16143970#16143970 – Clyde Apr 22 '13 at 09:47

1 Answers1

0

I don't know whether LINQ to Entities supports custom formatting, but you could try

Time= d.TimeFrom.ToString("HH:mm") + "-" + d.TimeTo.ToString("HH:mm")

However, I'd personally keep the values as DateTime values in the LINQ to Entities part, and only transform to text in the .NET code. In particular, different cultures have different preferred ways of representing times, so you should probably respect that. Separate "fetching the data" from "presenting the data".

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • So the `CultureInfo` would be the one of the connection to the SQL server? – xanatos Oct 19 '11 at 10:14
  • Ooops, I forgot to mention that custom formatting doesn't work in LINQ-To-Entities. That's the primary problem. Cultures aren't the problem as the web app will be used in only one country. – wegelagerer Oct 19 '11 at 10:15
  • @xanatos Yeah, the culture info is from the SQL server. – wegelagerer Oct 19 '11 at 10:16
  • 1
    @xanatos: I would avoid relying on the culture used to talk to SQL completely, if possible. The ASP.NET server should be the one that cares about presenting the data in a culturally-sensitive way, IMO. – Jon Skeet Oct 19 '11 at 10:17
  • 1
    @Hrvach: So just fetch the data as a DateTime, and do the formatting in your app instead, as I suggested. – Jon Skeet Oct 19 '11 at 10:18
  • @JonSkeet My question was different. But if ToString doesn't work it's a moot point (my question was: if the ToString is done server side, what culture will be used? Clearly not the one of the "client app". Probably the one of the sql user/the one set up in the connection string) – xanatos Oct 19 '11 at 10:20
  • @JonSkeet I don't know how to format it within the query otherwise I'd do it. I've seen couple of similar questions here on SO, but they weren't helpful to me. I agree that's the solution, but how to get to it is a different story. :) – wegelagerer Oct 19 '11 at 10:21
  • @Hrvach: No, you *shouldn't* be formatting within the query. That's my whole point. Fetch the data, then within the ASP.NET app, format it. That way your formatting is completely divorced from Entity Framework. – Jon Skeet Oct 19 '11 at 10:22
  • @JonSkeet Check this link: http://stackoverflow.com/questions/1772753/linq-to-entities-format-date-in-select-query-expression It is very similar to my problem with one difference and that is it has only one element in query and I have couple of them. Don't get me wrong, but I'd like to accomplish this the same way (if possible). – wegelagerer Oct 19 '11 at 10:27
  • @Hrvach: Note the call to `ToList()` in the accepted answer that question - which makes the formatting occur in .NET, not in Entity Framework, just as I've been suggesting... – Jon Skeet Oct 19 '11 at 10:37