如何在 LINQ 中设置字符串的格式
本文关键字:字符串 格式 设置 LINQ | 更新日期: 2023-09-27 18:30:46
我的时间串有问题..我想格式化它,以便它显示更整洁的设计.. 谁能帮我解决这个问题
这是我的代码:
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());
在我看来:
<input type="text" id="txtStartTime" name="txtStartTime" class="inputLong"
value="@ViewBag.startTime" disabled="disabled"/>
尝试在查询后调用First
:
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
或者也许更干净:
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;