【问题标题】:Displaying by DateTime.now - Silverlight WP7按 DateTime.now 显示 - Silverlight WP7
【发布时间】:2011-08-08 01:41:15
【问题描述】:

好的,所以我想显示一些属性,但前提是它们的开始时间是今天的日期。

有没有办法使用通配符来表示值?还是在 Where 子句中列出多个值?

这是一个例子..我在想是否可以拉出当前时间然后转换为字符串( var time = DateTime.Now.ToString("yyyyMMddHHmmss");) 我可以使用该 var 作为过滤器属性。这里 ... where tv.Attribute("start").Value == time

编辑**

我已经更新了代码,使其更清楚我所追求的。正如您在下面看到的,我使用 Where 子句来显示与特定“开始”时间相关的属性。

现在这个例子对我不起作用,因为首先它需要是 24 小时内的所有列表,而不是下面的特定时间,还因为我想按 DateTime.Now 动态显示列表。

所以,我真正需要的是一种显示当前日期的方法,但不是将特定时间显示为布尔值,并在我的布尔方程中使用它来显示当前日期的“开始”属性。

            WebClient c = new WebClient();
            c.DownloadStringCompleted += new DownloadStringCompletedEventHandler(c_DownloadStringCompleted);
            c.DownloadStringAsync(new Uri("http://www.domain.com/source.xml"));
            progressBar1.IsIndeterminate = true;
            progressBar1.Visibility = Visibility.Visible;


        } 


        void c_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
                return;


            var r = XDocument.Parse(e.Result);


            listBox2.ItemsSource = from tv in r.Root.Descendants("programme")
                                   where tv.Attribute("start").Value == "20110724190000 +1200"
                                   let channelE1 = tv.Attribute("channel")
                                   let startE1 = tv.Attribute("start")
                                   let nameEl = tv.Element("title")
                                   orderby tv.Attribute("start").Value ascending
                                   let urlEl = tv.Element("desc")


                                   select new TV1guide



                                   {
                                       DisplayName = nameEl == null ? null : nameEl.Value,
                                       ChannelName = channelE1 == null ? null : channelE1.Value,
                                       ChannelURL = urlEl == null ? null : urlEl.Value,
                                       StartTime = startE1 == null ? (DateTime?)null : DateTime.ParseExact(startE1.Value, "yyyyMMddHHmmss zzz", DateTimeFormatInfo.CurrentInfo, DateTimeStyles.AssumeLocal),



                                   };

            progressBar1.IsIndeterminate = false;
            progressBar1.Visibility = Visibility.Collapsed;

public class TV1guide
        {
            public string DisplayName { get; set; }
            public string ChannelURL { get; set; }
            public string ImageSource { get; set; }
            public DateTime? StartTime { get; set; }
            public DateTime? EndTime { get; set; }
            public string ChannelName { get; set; }

        }

    }

}

除此之外,我还尝试了 HiTech Magic 的建议,但我很确定语法是错误的。

                WebClient c = new WebClient();
                c.DownloadStringCompleted += new DownloadStringCompletedEventHandler(c_DownloadStringCompleted);
                c.DownloadStringAsync(new Uri("http://www.domain.com/source.xml"));
                progressBar1.IsIndeterminate = true;
                progressBar1.Visibility = Visibility.Visible;


            } 

bool MyDateCheckingMethod( DateTime otherDate )
{
    // Is this today (ignoring time)?
    return otherDate.Date == DateTime.Now.Date;
}

void c_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
            {
                if (e.Error != null)
                    return;


                var r = XDocument.Parse(e.Result);


                listBox2.ItemsSource = from tv in r.Root.Descendants("programme")
                                       where MyDateCheckingMethod(tv.Attribute("start").Value)
                                       let channelE1 = tv.Attribute("channel")
                                       let startE1 = tv.Attribute("start")
                                       let nameEl = tv.Element("title")
                                       orderby tv.Attribute("start").Value ascending
                                       let urlEl = tv.Element("desc")


                                       select new TV1guide



                                       {
                                           DisplayName = nameEl == null ? null : nameEl.Value,
                                           ChannelName = channelE1 == null ? null : channelE1.Value,
                                           ChannelURL = urlEl == null ? null : urlEl.Value,
                                           StartTime = startE1 == null ? (DateTime?)null : DateTime.ParseExact(startE1.Value, "yyyyMMddHHmmss zzz", DateTimeFormatInfo.CurrentInfo, DateTimeStyles.AssumeLocal),



                                       };

                progressBar1.IsIndeterminate = false;
                progressBar1.Visibility = Visibility.Collapsed;

    public class TV1guide
            {
                public string DisplayName { get; set; }
                public string ChannelURL { get; set; }
                public string ImageSource { get; set; }
                public DateTime? StartTime { get; set; }
                public DateTime? EndTime { get; set; }
                public string ChannelName { get; set; }

            }

        }

    }

【问题讨论】:

    标签: silverlight windows-phone-7 linq-to-xml where


    【解决方案1】:

    LINQ 可以很好地兼容所有 C# 函数,因此它可以很简单:

    where MyListOfDates.Contains(tv.Attribute("start").Value)
    

    只需将所有必需的日期放入List<DateTime> MyListOfDates

    如果您想使用其他比较而不仅仅是一个基本列表,请将逻辑放入一个方法中并调用它,例如:

    where MyDateCheckingMethod(tv.Attribute("start").Value)
    

    您从 XML 解析中获得的“日期/时间”值实际上是一个“yyyyMMddHHmmss K”格式的字符串,因此您的方法可能如下所示:

    bool MyDateCheckingMethod(string dateString)
    {
        DateTime otherDate = DateTime.ParseExact(dateString, "yyyyMMddHHmmss K", null);
        // Is this today (ignoring time)?
        return otherDate.Date == DateTime.Now.Date;
    }
    

    【讨论】:

    • 这意味着一天中的每个小时都有一个 48, 2 的列表,因为我希望它每天显示所有列表。这也意味着我必须每天更换它。有没有办法使用 DateTime.Now 并使用当前时间的变体?
    • @Rhys:请看第二部分。您可以进行“任何”比较,以便自己发明。只需将逻辑放在一个方法中和/或仅使用日期时间的一部分Date
    • 好的,谢谢。不过,我将如何在 Where 子句中使用它?我试过 where tv.Attribute("start").Value == MydateCheckingMethod 但我得到错误。感谢您在这方面的帮助。
    • @Rhys:您能否为您的问题添加一个更新,以准确显示您正在尝试什么?这样更容易纠正。
    • 我刚刚更新了我的代码并试图阐明我想要实现的目标
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-08
    • 1970-01-01
    • 2011-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多