如何使用Where子句匹配DateTime条件

时间:2009-08-26 21:30:58

标签: xml vb.net linq

以下查询适用于我,直到我对Where的{​​{1}}条件应用属性Hour = Id

我只需要获取今天日期的最高温度,而且我无法在日期字段中使用'06/02/2009 12:00CDT'条件。关于我做错了什么想法?

Where

下面是xml:

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

1 个答案:

答案 0 :(得分:1)

夫妻俩:

  1. 我没有看到County =“Adams”的任何元素。你给的文件是county =“Adair”。

  2. 我认为您正在尝试对看起来像<wind_gust>15</wind_gust>的字符串执行CInt()。

  3. County Adair和12:00CDT没有<wind_gust>元素。所以我在15:00 CDT查询,这给了我一个wind_gust元素。如果您查询没有wind_gust元素的时间,您将获得空集。

  4. 你说你想要最大温度,但你实际上是在询问wind_gust。我没有看到任何名为max_temp的元素。

  5. 此代码适用于我:

    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