LINQ to XML : A query body must end with a select clause or a group clause

前端 未结 6 1124
广开言路
广开言路 2021-01-20 01:31

Can someone guide me on to repairing the error on this query :

       var objApps = from item in xDoc.Descendants(\"VHost\") 
                          where         


        
相关标签:
6条回答
  • 2021-01-20 02:11

    The compiler is complaining about this part

    from x in item.Descendants("Application")
    

    inside your Where clause. You should change it so that

    • there is a select clause at the end, and
    • it makes up an expression that evaluates to true for item objects that you would like to keep.

    Here is my best guess at what you are trying to do (EDIT : attempt number two)

    var objApps = from item in xDoc.Descendants("VHost").Descendants("Application") 
                  select new clsApplication {
                      ConnectionsTotal = item.Element("ConnectionsTotal").Value
                  };
    
    0 讨论(0)
  • 2021-01-20 02:16

    I like the procedural way of linq which can be more linear in its approach; I believe you want

    var result =   xDoc.Descendants("VHost")
                       .Descendants("ConnectionsTotal")
                       .Select(ele => ele.Value )
                       .Select( value => new clsApplication
                               {
                                   ConnectionsTotal = value
                               })
                             ;
    

    --- Test in LinqPad ---

    string xml = @"
    <Data>
    <Server>
      <ConnectionsCurrent>67</ConnectionsCurrent> 
      <ConnectionsTotal>1424182</ConnectionsTotal> 
      <ConnectionsTotalAccepted>1385091</ConnectionsTotalAccepted> 
      <ConnectionsTotalRejected>39091</ConnectionsTotalRejected> 
      <MessagesInBytesRate>410455.0</MessagesInBytesRate> 
      <MessagesOutBytesRate>540146.0</MessagesOutBytesRate> 
     </Server>
     <VHost>
      <Name>_defaultVHost_</Name> 
      <TimeRunning>5129615.178</TimeRunning> 
      <ConnectionsLimit>0</ConnectionsLimit> 
      <ConnectionsCurrent>67</ConnectionsCurrent> 
      <ConnectionsTotal>1424182</ConnectionsTotal> 
      <ConnectionsTotalAccepted>1385091</ConnectionsTotalAccepted> 
      <ConnectionsTotalRejected>39091</ConnectionsTotalRejected> 
      <MessagesInBytesRate>410455.0</MessagesInBytesRate> 
      <MessagesOutBytesRate>540146.0</MessagesOutBytesRate> 
     </VHost>
     <Application>
      <Name>TestApp</Name> 
      <Status>loaded</Status> 
      <TimeRunning>411642.953</TimeRunning> 
      <ConnectionsCurrent>11</ConnectionsCurrent> 
      <ConnectionsTotal>43777</ConnectionsTotal> 
      <ConnectionsTotalAccepted>43135</ConnectionsTotalAccepted> 
      <ConnectionsTotalRejected>642</ConnectionsTotalRejected> 
      <MessagesInBytesRate>27876.0</MessagesInBytesRate> 
      <MessagesOutBytesRate>175053.0</MessagesOutBytesRate></Application>
      </Data>";
    
    
      var XDoc = XDocument.Parse(xml);
    
      XDoc.Descendants("VHost")
          .Descendants("ConnectionsTotal")
          .Select (ele => ele.Value )
          .Dump();  
    
    0 讨论(0)
  • 2021-01-20 02:22

    The error is complaining about your inner from clause (inside the where) it looks like you're trying to do a select many (unintentional clippy reference).

    var objApps = from item in xDoc.Descendants("VHost") 
                  from x in item.Descendants("Application")
                  select new clsApplication
                           {
                               ConnectionsTotal = x.Element("ConnectionsTotal").Value
                           };
    
    0 讨论(0)
  • 2021-01-20 02:32

    You are missing select in query within where clause, you can change it something like

    var objApps = from item in xDoc.Descendants("VHost") 
                         where(item.Descendants("Application").Any())
                         select new clsApplication
                         {
                               ConnectionsTotal = item.Element("Application").Element("ConnectionsTotal").Value
                         };
    
    0 讨论(0)
  • 2021-01-20 02:32

    Simply do the following, no need for the where clause. Descendants will search children at ALL levels, not just immediately below:

    var objApps = from item in xDoc.Descendants("Application") 
                       select new clsApplication
                       {
                           ConnectionsTotal = item.Element("ConnectionsTotal").Value
                       };
    
    0 讨论(0)
  • 2021-01-20 02:36

    This may help:

            var objApps = from item in xDoc.Descendants("VHost")
                           from x in item.Descendants("Application")
                          select new clsApplication
                       {
                           ConnectionsTotal = item.Element("Application").Element("ConnectionsTotal").Value
                       };
    
    0 讨论(0)
提交回复
热议问题