【问题标题】:Format date in linq query WP7在linq查询WP7中格式化日期
【发布时间】:2012-05-30 12:13:15
【问题描述】:

我有一个下面显示的 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” 谢谢

【问题讨论】:

  • 您好,您的日期字段是什么类型的?

标签: xml linq windows-phone-7


【解决方案1】:

您不能将 Direct Cast 与 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"),

          };

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    • 2015-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    相关资源
    最近更新 更多