tags:

views:

975

answers:

4

Hi, I am new in LINQ world. I need an urgent help in reading the xml elements using LINQ with specific where condition.

I need to find the max air_temp for a county i.e where county name = "Boone" and hour id = "06/03/2009 09:00CDT"

i tried something like below, but no luck :

 Dim custs As IEnumerable = From c In Element.Load("C:\meridian.xml").Elements("county") _
                            Select c.Elements("hour").Elements("air_temp").Max()

 For Each x In custs
        Response.Write(custs(0).ToString())
 Next

------------------- here is the xml file :

 <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>
+1  A: 
Dim document = XDocument.Load("meridian.xml")
Dim air_temps = From county In document.Root.Elements("county") _
                From hour in county.Elements("hour") _
                From air_temp in hour.Elements("air_temp") _
                Select CInt(air_temp)
Dim max_air_temp = air_temps.Max()

Using XML literals:

Dim forecasts = XElement.Load("meridian.xml")
Dim air_temps = From air_temp In forecasts.<county>.<hour>.<air_temp> _
                Select CInt(air_temp)
Dim max_air_temp = air_temps.Max()
Joe Chung
+1  A: 

I assume that you need "country / max temp. date / max temp. value" aggregates as an output. If so, this will do the trick:

Dim meridian = XDocument.Load("meridian.xml")
Dim maxByCounty = _
    From county In meridian.<forecasts>.<county> _
    Let maxHour = (From hour In county.<hour> _
                   Order By CType(hour.<air_temp>, Integer) Desescending _
                  ).First _
    Select New With { .Name = county.<name>, _
                      .Hour = maxHour.@id, 
                      .AirTemp = maxHour.<air_temp> }

And then you can use the result like this:

For Each m In maxByCounty
    Console.WriteLine(m.Name, m.Hour, m.AirTemp)
Next
Pavel Minaev
A: 

Thanks for the quick reply , I tried below one & it works fine for me untill i apply where condition for Hour with attribute Id = '06/02/2009 12:00CDT'

i need to get the max temp for today's date only... & i am not able to use where condition in date field. Please help, or suggest me what i m doing wrong.


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
New Linq Baby
+1  A: 

Shouldn't it be

From hour In county.Elements("hour") Where hour.Attribute("id")...

instead of

From hour In county.Elements("hour") Where county.Element("hour").Attribute("id")...

?

Also, you shouldn't post replies to other posts as additional answers.

Adam Robinson