LINQ to XML:查询主体必须以select子句或group子句结束

时间:2012-12-04 16:29:08

标签: c# linq

有人可以指导我修复此查询的错误:

       var objApps = from item in xDoc.Descendants("VHost") 
                          where(from x in item.Descendants("Application"))
                          select new clsApplication
                       {
                           ConnectionsTotal = item.Element("ConnectionsTotal").Value
                       };

它显示编译器错误“查询正文必须以select子句或group子句结束”。我哪里错了?

非常感谢任何帮助..

感谢。

编辑:这是我的XML(这里没有关闭标签)...我需要Application中的connectioncount值..

    - <Server>
  <ConnectionsCurrent>67</ConnectionsCurrent> 
  <ConnectionsTotal>1424182</ConnectionsTotal> 
  <ConnectionsTotalAccepted>1385091</ConnectionsTotalAccepted> 
  <ConnectionsTotalRejected>39091</ConnectionsTotalRejected> 
  <MessagesInBytesRate>410455.0</MessagesInBytesRate> 
  <MessagesOutBytesRate>540146.0</MessagesOutBytesRate> 
- <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> 
- <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>

6 个答案:

答案 0 :(得分:4)

编译器抱怨这部分

from x in item.Descendants("Application")

Where子句中。你应该改变它以便

  • 最后有一个select子句,
  • 它构成一个表达式,对于您希望保留的true个对象,评估为item

以下是我对你要做的事情的最好猜测(编辑:尝试排名第二)

var objApps = from item in xDoc.Descendants("VHost").Descendants("Application") 
              select new clsApplication {
                  ConnectionsTotal = item.Element("ConnectionsTotal").Value
              };

答案 1 :(得分:3)

错误是抱怨你的内部from子句(在where里面)看起来你正在尝试选择多个(无意的clippy引用)。

var objApps = from item in xDoc.Descendants("VHost") 
              from x in item.Descendants("Application")
              select new clsApplication
                       {
                           ConnectionsTotal = x.Element("ConnectionsTotal").Value
                       };

答案 2 :(得分:2)

您在where子句中缺少select in query,您可以将其更改为

var objApps = from item in xDoc.Descendants("VHost") 
                     where(item.Descendants("Application").Any())
                     select new clsApplication
                     {
                           ConnectionsTotal = item.Element("Application").Element("ConnectionsTotal").Value
                     };

答案 3 :(得分:2)

只需执行以下操作,无需where子句。后代将搜索所有级别的儿童,而不仅仅是在下面:

var objApps = from item in xDoc.Descendants("Application") 
                   select new clsApplication
                   {
                       ConnectionsTotal = item.Element("ConnectionsTotal").Value
                   };

答案 4 :(得分:1)

这可能有所帮助:

        var objApps = from item in xDoc.Descendants("VHost")
                       from x in item.Descendants("Application")
                      select new clsApplication
                   {
                       ConnectionsTotal = item.Element("Application").Element("ConnectionsTotal").Value
                   };

答案 5 :(得分:1)

我喜欢linq的程序方式,它的方法更加线性;我相信你想要

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

---在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();