2009-08-26 51 views
0

下面的查詢適用於我,直到我應用Where條件Hour的屬性Id = '06/02/2009 12:00CDT'如何使用Where子句匹配DateTime條件

我只需要獲取當天日期的最大溫度,而且我無法在日期字段中使用Where條件。關於我做錯什麼的想法?

Dim document = XDocument.Load("c:\meridian.xml") 
Dim wind_gusts = From county In document.Root.Elements("county") Where county.Attribute("name") = "Adams" _ 
From hour In county.Elements("hour") Where county.Element("hour").Attribute("id").Value = "06/02/2009 13:00CDT" _ 
From wind_gust In hour.Elements("wind_gust") _ 
Select CInt(wind_gust) '< DateTime.Parse("06/03/2009") _ 
'DateTime.Parse(county.Element("hour" + ID).Attribute("id")) 
'Dim wind_gust_value = wind_gusts.Max() 

For Each m In wind_gusts 
    Response.Write(m.ToString()) 
Next 

下面是XML:

<?xml version="1.0" ?> 
- <forecasts> 
    <issued>06/02/2009 12:00CDT</issued> 
- <county name="Adair"> 
- <hour id="06/02/2009 12:00CDT"> 
    <air_temp>61</air_temp> 
    <cloud_cover>overcast</cloud_cover> 
    <dew_point>59</dew_point> 
    <precip_prob>90</precip_prob> 
    <precip_rate>0.12</precip_rate> 
    <precip_type>rain</precip_type> 
    <snow_rate>0.0</snow_rate> 
    <wind_direction>NE</wind_direction> 
    <wind_speed>12</wind_speed> 
    <dew_point_confidence>-3/+3</dew_point_confidence> 
    <road_temp>64</road_temp> 
    <road_frost_prob>0</road_frost_prob> 
    <road_potential_evap_rate>429</road_potential_evap_rate> 
    <road_temp_confidence>-3/+2</road_temp_confidence> 
    <dew_point_confidence>-3/+3</dew_point_confidence> 
    <bridge_temp>63</bridge_temp> 
    <bridge_temp_confidence>-4/+2</bridge_temp_confidence> 
    <bridge_frost>NO</bridge_frost> 
    </hour> 
+ <hour id="06/02/2009 13:00CDT"> 
    <air_temp>61</air_temp> 
    <cloud_cover>overcast</cloud_cover> 
    <dew_point>60</dew_point> 
    <precip_prob>70</precip_prob> 
    <precip_rate>0.01</precip_rate> 
    <precip_type>rain</precip_type> 
    <snow_rate>0.0</snow_rate> 
    <wind_direction>ENE</wind_direction> 
    <wind_speed>10</wind_speed> 
    <dew_point_confidence>-3/+3</dew_point_confidence> 
    <road_temp>65</road_temp> 
    <road_frost_prob>0</road_frost_prob> 
    <road_potential_evap_rate>411</road_potential_evap_rate> 
    <road_temp_confidence>-3/+2</road_temp_confidence> 
    <dew_point_confidence>-3/+3</dew_point_confidence> 
    <bridge_temp>64</bridge_temp> 
    <bridge_temp_confidence>-4/+1</bridge_temp_confidence> 
    <bridge_frost>NO</bridge_frost> 
    </hour> 
+ <hour id="06/02/2009 14:00CDT"> 
    <air_temp>64</air_temp> 
    <cloud_cover>overcast</cloud_cover> 
    <dew_point>60</dew_point> 
    <precip_prob>60</precip_prob> 
    <precip_rate>trace</precip_rate> 
    <precip_type>rain</precip_type> 
    <snow_rate>0.0</snow_rate> 
    <wind_direction>NE</wind_direction> 
    <wind_speed>12</wind_speed> 
    <dew_point_confidence>-3/+3</dew_point_confidence> 
    <road_temp>65</road_temp> 
    <road_frost_prob>0</road_frost_prob> 
    <road_potential_evap_rate>560</road_potential_evap_rate> 
    <road_temp_confidence>-3/+2</road_temp_confidence> 
    <dew_point_confidence>-3/+3</dew_point_confidence> 
    <bridge_temp>65</bridge_temp> 
    <bridge_temp_confidence>-3/+2</bridge_temp_confidence> 
    <bridge_frost>NO</bridge_frost> 
    </hour> 
+ <hour id="06/02/2009 15:00CDT"> 
    <air_temp>63</air_temp> 
    <cloud_cover>overcast</cloud_cover> 
    <dew_point>59</dew_point> 
    <precip_prob>50</precip_prob> 
    <precip_rate>trace</precip_rate> 
    <precip_type>rain</precip_type> 
    <snow_rate>0.0</snow_rate> 
    <wind_direction>NE</wind_direction> 
    <wind_gust>15</wind_gust> 
    <wind_speed>12</wind_speed> 
    <dew_point_confidence>-3/+3</dew_point_confidence> 
    <road_temp>63</road_temp> 
    <road_frost_prob>0</road_frost_prob> 
    <road_potential_evap_rate>397</road_potential_evap_rate> 
    <road_temp_confidence>-4/+3</road_temp_confidence> 
    <dew_point_confidence>-3/+3</dew_point_confidence> 
    <bridge_temp>63</bridge_temp> 
    <bridge_temp_confidence>-3/+2</bridge_temp_confidence> 
    <bridge_frost>NO</bridge_frost> 
    </hour> 
+ <hour id="06/02/2009 16:00CDT"> 
    <air_temp>64</air_temp> 
    <cloud_cover>overcast</cloud_cover> 
    <dew_point>57</dew_point> 
    <precip_prob>40</precip_prob> 
    <precip_rate>trace</precip_rate> 
    <precip_type>rain</precip_type> 
    <snow_rate>0.0</snow_rate> 
    <wind_direction>ENE</wind_direction> 
    <wind_gust>16</wind_gust> 
    <wind_speed>11</wind_speed> 
    <dew_point_confidence>-3/+3</dew_point_confidence> 
    <road_temp>62</road_temp> 
    <road_frost_prob>0</road_frost_prob> 
    <road_potential_evap_rate>400</road_potential_evap_rate> 
    <road_temp_confidence>-3/+3</road_temp_confidence> 
    <dew_point_confidence>-3/+3</dew_point_confidence> 
    <bridge_temp>61</bridge_temp> 
    <bridge_temp_confidence>-4/+2</bridge_temp_confidence> 
    <bridge_frost>NO</bridge_frost> 
    </hour> 
+ <hour id="06/02/2009 17:00CDT"> 
    <air_temp>63</air_temp> 
    <cloud_cover>overcast</cloud_cover> 
    <dew_point>56</dew_point> 
    <precip_prob>30</precip_prob> 
    <precip_rate>trace</precip_rate> 
    <precip_type>rain</precip_type> 
    <snow_rate>0.0</snow_rate> 
    <wind_direction>ENE</wind_direction> 
    <wind_gust>15</wind_gust> 
    <wind_speed>10</wind_speed> 
    <dew_point_confidence>-3/+3</dew_point_confidence> 
    <road_temp>61</road_temp> 
    <road_frost_prob>0</road_frost_prob> 
    <road_potential_evap_rate>344</road_potential_evap_rate> 
    <road_temp_confidence>-3/+3</road_temp_confidence> 
    <dew_point_confidence>-3/+3</dew_point_confidence> 
    <bridge_temp>60</bridge_temp> 
    <bridge_temp_confidence>-4/+2</bridge_temp_confidence> 
    <bridge_frost>NO</bridge_frost> 
    </hour> 
+ <hour id="06/02/2009 18:00CDT"> 
    <air_temp>62</air_temp> 
    <cloud_cover>mostly cloudy</cloud_cover> 
    <dew_point>54</dew_point> 
    <precip_prob>20</precip_prob> 
    <precip_rate>trace</precip_rate> 
    <precip_type>rain</precip_type> 
    <snow_rate>0.0</snow_rate> 
    <wind_direction>ENE</wind_direction> 
    <wind_speed>10</wind_speed> 
    <dew_point_confidence>-3/+4</dew_point_confidence> 
    <road_temp>61</road_temp> 
    <road_frost_prob>0</road_frost_prob> 
    <road_potential_evap_rate>530</road_potential_evap_rate> 
    <road_temp_confidence>-3/+3</road_temp_confidence> 
    <dew_point_confidence>-3/+4</dew_point_confidence> 
    <bridge_temp>61</bridge_temp> 
    <bridge_temp_confidence>-3/+2</bridge_temp_confidence> 
    <bridge_frost>NO</bridge_frost> 
    </hour> 
+ <hour id="06/02/2009 19:00CDT"> 
    <air_temp>61</air_temp> 
    <cloud_cover>mostly cloudy</cloud_cover> 
    <dew_point>52</dew_point> 
    <precip_prob>10</precip_prob> 
    <precip_rate>trace</precip_rate> 
    <precip_type>rain</precip_type> 
    <snow_rate>0.0</snow_rate> 
    <wind_direction>ENE</wind_direction> 
    <wind_speed>7</wind_speed> 
    <dew_point_confidence>-3/+4</dew_point_confidence> 
    <road_temp>68</road_temp> 
    <road_frost_prob>0</road_frost_prob> 
    <road_potential_evap_rate>959</road_potential_evap_rate> 
    <road_temp_confidence>-3/+3</road_temp_confidence> 
    <dew_point_confidence>-3/+4</dew_point_confidence> 
    <bridge_temp>66</bridge_temp> 
    <bridge_temp_confidence>-3/+3</bridge_temp_confidence> 
    <bridge_frost>NO</bridge_frost> 
    </hour> 
+ <hour id="06/02/2009 20:00CDT"> 
    <air_temp>60</air_temp> 
    <cloud_cover>mostly cloudy</cloud_cover> 
    <dew_point>51</dew_point> 
    <precip_prob>10</precip_prob> 
    <precip_rate>trace</precip_rate> 
    <precip_type>rain</precip_type> 
    <snow_rate>0.0</snow_rate> 
    <wind_direction>ENE</wind_direction> 
    <wind_speed>5</wind_speed> 
    <dew_point_confidence>-3/+4</dew_point_confidence> 
    <road_temp>65</road_temp> 
    <road_frost_prob>0</road_frost_prob> 
    <road_potential_evap_rate>495</road_potential_evap_rate> 
    <road_temp_confidence>-3/+4</road_temp_confidence> 
    <dew_point_confidence>-3/+4</dew_point_confidence> 
    <bridge_temp>62</bridge_temp> 
    <bridge_temp_confidence>-3/+3</bridge_temp_confidence> 
    <bridge_frost>NO</bridge_frost> 
    </hour> 
+ <hour id="06/02/2009 21:00CDT"> 
    <air_temp>57</air_temp> 
    <cloud_cover>mostly cloudy</cloud_cover> 
    <dew_point>51</dew_point> 
    <precip_prob>0</precip_prob> 
    <precip_rate>0.00</precip_rate> 
    <precip_type>none</precip_type> 
    <snow_rate>0.0</snow_rate> 
    <wind_direction>NE</wind_direction> 
    <wind_speed>5</wind_speed> 
    <dew_point_confidence>-3/+4</dew_point_confidence> 
    <road_temp>62</road_temp> 
    <road_frost_prob>0</road_frost_prob> 
    <road_potential_evap_rate>339</road_potential_evap_rate> 
    <road_temp_confidence>-3/+4</road_temp_confidence> 
    <dew_point_confidence>-3/+4</dew_point_confidence> 
    <bridge_temp>59</bridge_temp> 
    <bridge_temp_confidence>-3/+3</bridge_temp_confidence> 
    <bridge_frost>NO</bridge_frost> 
    </hour> 
+ <hour id="06/02/2009 22:00CDT"> 
    <air_temp>56</air_temp> 
    <cloud_cover>mostly cloudy</cloud_cover> 
    <dew_point>51</dew_point> 
    <precip_prob>0</precip_prob> 
    <precip_rate>0.00</precip_rate> 
    <precip_type>none</precip_type> 
    <snow_rate>0.0</snow_rate> 
    <wind_direction>ENE</wind_direction> 
    <wind_speed>5</wind_speed> 
    <dew_point_confidence>-3/+4</dew_point_confidence> 
    <road_temp>60</road_temp> 
    <road_frost_prob>0</road_frost_prob> 
    <road_potential_evap_rate>309</road_potential_evap_rate> 
    <road_temp_confidence>-3/+4</road_temp_confidence> 
    <dew_point_confidence>-3/+4</dew_point_confidence> 
    <bridge_temp>58</bridge_temp> 
    <bridge_temp_confidence>-3/+3</bridge_temp_confidence> 
    <bridge_frost>NO</bridge_frost> 
    </hour> 
+0

這是爲什麼社會維基?如果他們無法獲得代表,人們不會想要幫助你解決問題。 – 2009-08-26 21:40:05

+0

如果你想要max temp,爲什麼要查詢wind_gust? – Cheeso 2009-08-26 21:58:38

回答

1

兩件事情:

  1. 我沒有看到縣= 「亞當斯」 的任何元素。你給的文件有縣=「Adair」。

  2. 我想你正在試圖對一個看起來像<wind_gust>15</wind_gust>的字符串做一個CInt()。

  3. 阿代爾縣和12:00CDT沒有<wind_gust>元素。所以我在15:00CDT上詢問,這給了我一個wind_gust元素。如果您在沒有wind_gust元素的時間查詢,您將獲得空集。

  4. 你說你想要最高溫度,但你實際上是在查詢wind_gust。我沒有看到任何稱爲max_temp的元素。

此代碼的工作對我來說:

Public Sub QueryWindGust(ByVal county As String, ByVal hour As String) 
    Dim document = XDocument.Load("meridian.xml") 
    Dim selection = From c In document.Root.Elements("county") _ 
     Where c.Attribute("name").Value = "Adair" _ 
     From h In c.Elements("hour") _ 
     Where h.Attribute("id").Value = hour _ 
     From g In h.Elements("wind_gust") _ 
     Select CInt(g.Value) 


    Console.WriteLine("For county {0}, hour ({1}):", county, hour) 
    If (selection.Count > 0) 
     Console.Write("wind gust: ") 
     For Each m In selection 
      Console.WriteLine(m.ToString() & ": (" & m.GetType().ToString() & ")") 
     Next 
    Else 
     Console.WriteLine("No elements returned.") 
    End If 
End Sub 

Public Sub Run() 
    QueryWindGust("Adams","06/02/2009 12:00CDT") 
    QueryWindGust("Adair","06/02/2009 12:00CDT") 
    QueryWindGust("Adair","06/02/2009 15:00CDT") 
End Sub