1

im having problem with my string of time.. i wanted to format it so that it will display a more neat design.. can anyone help me with this one

here's my code:

 ViewBag.startTime = (from a in test where a.ID == clientCustomerPositionShiftnfo.ID select new{ a.StartTime}) .AsEnumerable() .Select(a => a.StartTime != "Anytime" ? Convert.ToDateTime(a.StartTime).ToString("HH:mm:ss") : a.StartTime.Trim()); 

In my view:

 <input type="text" id="txtStartTime" name="txtStartTime" class="inputLong" value="@ViewBag.startTime" disabled="disabled"/> 
4
  • 1
    Why the 'select new', why not just select the property? Commented Sep 9, 2013 at 2:50
  • in my view it doesnt show the value of the time instead it shows something like this "System.Linq.Enumerable+WhereSelectEnumerableIterator2[<>f__AnonymousTypeb1[System.String],System.String]" Commented Sep 9, 2013 at 2:50
  • @tuespetre when i select the property i get the value but i cant format it... i need a way to format the starttime.. Commented Sep 9, 2013 at 2:53
  • A cleaner design would be if the view formats the date using JavaScript and the browser's or users local settings. Commented Sep 9, 2013 at 2:53

1 Answer 1

3

Try calling First after your query:

ViewBag.startTime = (from a in test where a.ID == clientCustomerPositionShiftnfo.ID select a.StartTime) .AsEnumerable() .Select(t => t != "Anytime" ? Convert.ToDateTime(t).ToString("HH:mm:ss") : t) .First(); // or FirstOrDefault if your query might not return any results 

Or perhaps more cleanly:

var startTime = (from a in test where a.ID == clientCustomerPositionShiftnfo.ID select a.StartTime) .First(); // or FirstOrDefault if your query might not return any results ViewBag.startTime startTime != "Anytime" ? Convert.ToDateTime(startTime).ToString("HH:mm:ss") : startTime; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.