3

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

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

它显示编译器错误“查询主体必须以选择子句或组子句结尾”。我哪里错了?

将不胜感激任何帮助..

谢谢。

编辑:这是我的 XML(这里没有关闭标签)...我需要应用程序中的连接计数值..

    - <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>
4

6 回答 6

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
              };
于 2012-12-04T16:33:12.790 回答
3

该错误是在抱怨您的内部 from 子句(在 where 内),看起来您正在尝试进行多选(无意的剪辑参考)。

var objApps = from item in xDoc.Descendants("VHost") 
              from x in item.Descendants("Application")
              select new clsApplication
                       {
                           ConnectionsTotal = x.Element("ConnectionsTotal").Value
                       };
于 2012-12-04T16:34:04.417 回答
2

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

var objApps = from item in xDoc.Descendants("Application") 
                   select new clsApplication
                   {
                       ConnectionsTotal = item.Element("ConnectionsTotal").Value
                   };
于 2012-12-04T17:19:32.027 回答
2

您在 where 子句中的查询中缺少选择,您可以将其更改为

var objApps = from item in xDoc.Descendants("VHost") 
                     where(item.Descendants("Application").Any())
                     select new clsApplication
                     {
                           ConnectionsTotal = item.Element("Application").Element("ConnectionsTotal").Value
                     };
于 2012-12-04T16:31:42.070 回答
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
                   };
于 2012-12-04T16:35:40.943 回答
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();  
于 2012-12-04T16:37:04.347 回答