按DateTime.now显示 - Silverlight WP7

时间:2011-07-19 09:05:45

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

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

有没有办法使用通配符获取价值?或在Where子句中列出多个值?

这是一个例子..我在想是否可以拉动当前时间然后转换为字符串(var time = DateTime.Now.ToString(“yyyyMMddHHmmss”);)我可以使用该var作为过滤器属性。这里...其中tv.Attribute(“开始”)。值==时间

编辑 * *

我已经更新了代码,使我更清楚自己所追求的内容。如下所示,我使用Where子句显示与特定“开始”时间相关的属性。

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

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

            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; }

            }

        }

    }

1 个答案:

答案 0 :(得分:3)

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;
}