Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Help for writing a LINQ query !

Help for writing a LINQ query !

Scheduled Pinned Locked Moved LINQ
csharpdatabaselinqxmlhelp
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mohammad Dayyan
    wrote on last edited by
    #1

    Hello. I have a XML file like this :

    <XML>
    <Part ID="1" />
    <Part ID="2" />
    <Part ID="3" />
    <Part ID="4" />
    <Part ID="5" />
    <Part ID="6" />
    <Part ID="7" />
    .
    .
    .
    </XML>

    I'm gonna select the Part element with maximum ID. I've used this query, but it's wrong.

    XElement xElement = XElement.Parse(xmlSTR);
    var max = from q in xElement.Descendants("Part" )
    where q.Attribute("ID" ).Value == q.Attributes("ID" ).Max().Value
    select q;
    Console.WriteLine(max.First());

    what do you suggest ? thank you.

    H F 2 Replies Last reply
    0
    • M Mohammad Dayyan

      Hello. I have a XML file like this :

      <XML>
      <Part ID="1" />
      <Part ID="2" />
      <Part ID="3" />
      <Part ID="4" />
      <Part ID="5" />
      <Part ID="6" />
      <Part ID="7" />
      .
      .
      .
      </XML>

      I'm gonna select the Part element with maximum ID. I've used this query, but it's wrong.

      XElement xElement = XElement.Parse(xmlSTR);
      var max = from q in xElement.Descendants("Part" )
      where q.Attribute("ID" ).Value == q.Attributes("ID" ).Max().Value
      select q;
      Console.WriteLine(max.First());

      what do you suggest ? thank you.

      H Offline
      H Offline
      Howard Richards
      wrote on last edited by
      #2

      XAttribute.Value is always a string. You need to convert to an integer.

      XElement e = XElement.Parse(xmlSTR);

              // get elements as integers
              var values = from q in e.Descendants("Part")
                           select Convert.ToInt32(q.Attribute("ID").Value);
              Console.WriteLine("Found {0} elements", values.Count());
              
              // get highest value
              var highest = values.Max();
              Console.WriteLine("Highest value is {0}", highest);
      
              // get Part element with highest
              var highestElement = (from q in e.Descendants("Part")
                                    where q.Attribute("ID").Value == highest.ToString()
                                    select q).First();
      
              //report
              Console.WriteLine("Highest element: {0}", highestElement.ToString());
      
              Console.ReadKey();
      

      'Howard

      M F 2 Replies Last reply
      0
      • H Howard Richards

        XAttribute.Value is always a string. You need to convert to an integer.

        XElement e = XElement.Parse(xmlSTR);

                // get elements as integers
                var values = from q in e.Descendants("Part")
                             select Convert.ToInt32(q.Attribute("ID").Value);
                Console.WriteLine("Found {0} elements", values.Count());
                
                // get highest value
                var highest = values.Max();
                Console.WriteLine("Highest value is {0}", highest);
        
                // get Part element with highest
                var highestElement = (from q in e.Descendants("Part")
                                      where q.Attribute("ID").Value == highest.ToString()
                                      select q).First();
        
                //report
                Console.WriteLine("Highest element: {0}", highestElement.ToString());
        
                Console.ReadKey();
        

        'Howard

        M Offline
        M Offline
        Mohammad Dayyan
        wrote on last edited by
        #3

        Thank you. So we can't do it within one query ?

        H 1 Reply Last reply
        0
        • H Howard Richards

          XAttribute.Value is always a string. You need to convert to an integer.

          XElement e = XElement.Parse(xmlSTR);

                  // get elements as integers
                  var values = from q in e.Descendants("Part")
                               select Convert.ToInt32(q.Attribute("ID").Value);
                  Console.WriteLine("Found {0} elements", values.Count());
                  
                  // get highest value
                  var highest = values.Max();
                  Console.WriteLine("Highest value is {0}", highest);
          
                  // get Part element with highest
                  var highestElement = (from q in e.Descendants("Part")
                                        where q.Attribute("ID").Value == highest.ToString()
                                        select q).First();
          
                  //report
                  Console.WriteLine("Highest element: {0}", highestElement.ToString());
          
                  Console.ReadKey();
          

          'Howard

          F Offline
          F Offline
          Fernando Soto
          wrote on last edited by
          #4

          Hi Howard; This should do what you want.

          XElement xElement = XElement.Parse(xmlSTR);
          var max = (from q in xElement.Descendants("Part")
          let val = Convert.ToInt32(q.Attribute("ID").Value)
          orderby val descending
          select q).First();

          MessageBox.Show(max.ToString());

          Fernando

          H M 2 Replies Last reply
          0
          • F Fernando Soto

            Hi Howard; This should do what you want.

            XElement xElement = XElement.Parse(xmlSTR);
            var max = (from q in xElement.Descendants("Part")
            let val = Convert.ToInt32(q.Attribute("ID").Value)
            orderby val descending
            select q).First();

            MessageBox.Show(max.ToString());

            Fernando

            H Offline
            H Offline
            Howard Richards
            wrote on last edited by
            #5

            Thanks but it wasn't me that asked

            'Howard

            1 Reply Last reply
            0
            • M Mohammad Dayyan

              Thank you. So we can't do it within one query ?

              H Offline
              H Offline
              Howard Richards
              wrote on last edited by
              #6

              I wrote it as separate ones for clarity - you can just replace the variables in the later queries for the original query - just like subqueries in SQL for example.

              'Howard

              1 Reply Last reply
              0
              • F Fernando Soto

                Hi Howard; This should do what you want.

                XElement xElement = XElement.Parse(xmlSTR);
                var max = (from q in xElement.Descendants("Part")
                let val = Convert.ToInt32(q.Attribute("ID").Value)
                orderby val descending
                select q).First();

                MessageBox.Show(max.ToString());

                Fernando

                M Offline
                M Offline
                Mohammad Dayyan
                wrote on last edited by
                #7

                Great, thanks

                F 1 Reply Last reply
                0
                • M Mohammad Dayyan

                  Hello. I have a XML file like this :

                  <XML>
                  <Part ID="1" />
                  <Part ID="2" />
                  <Part ID="3" />
                  <Part ID="4" />
                  <Part ID="5" />
                  <Part ID="6" />
                  <Part ID="7" />
                  .
                  .
                  .
                  </XML>

                  I'm gonna select the Part element with maximum ID. I've used this query, but it's wrong.

                  XElement xElement = XElement.Parse(xmlSTR);
                  var max = from q in xElement.Descendants("Part" )
                  where q.Attribute("ID" ).Value == q.Attributes("ID" ).Max().Value
                  select q;
                  Console.WriteLine(max.First());

                  what do you suggest ? thank you.

                  F Offline
                  F Offline
                  Fernando Soto
                  wrote on last edited by
                  #8

                  Hi Mohammad; This should do what you want.

                  XElement xElement = XElement.Parse(xmlSTR);
                  var max = (from q in xElement.Descendants("Part")
                  let val = Convert.ToInt32(q.Attribute("ID").Value)
                  orderby val descending
                  select q).First();

                  MessageBox.Show(max.ToString());

                  Fernando

                  1 Reply Last reply
                  0
                  • M Mohammad Dayyan

                    Great, thanks

                    F Offline
                    F Offline
                    Fernando Soto
                    wrote on last edited by
                    #9

                    Not a problem, glad to help.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups