2012-05-30 155 views
-1

我有一个LINQ查询显示低于格式日期

XDocument data = XDocument.Parse(xml); 

      var persons = from query in data.Descendants("Table") 
          select new MailList 
          { 
           Sender = (string)query.Element("FromUser"), 
           Body = (string)query.Element("Message"), 

           Date = (string)query.Element("mDate"), 
           Time = (string)query.Element("mTime"), 

          }; 
      EmailList.ItemsSource = persons; 

我想格式化“MM/YY”日期和时间“HH:MM” 谢谢

+0

嗨,你的日期字段的类型是什么? –

+0

都是字符串 –

回答

0

您不能将DateTime作为值类型使用直接投射。如果日期格式不正确,请使用普通演员表,但要小心FormatException

var persons = from query in data.Descendants("Table") 
      select new MailList 
      { 
       Sender = (string)query.Element("FromUser"), 
       Body = (string)query.Element("Message"), 
       Date = ((DateTime)query.Element("mDate")).ToString("MM/yy"), 
       Time = ((DateTime)query.Element("mTime")).ToString("hh:mm"), 

      };