2010-12-07 74 views
1

我有以下XML,并且我需要一个查询,以便在通过特定统计信息时提取Year和Return_Value。如何在linq xml语句中使用where子句

当前的代码不起作用,因为在字典对象的一个​​添加语句中不能有两个where子句。有针对这个的解决方法吗?

foreach (var row in rows) 
{ 
    myHoldingsDistribution.Add(row.Descendants() 
     .Where(y => y.Name.LocalName == keyName) 
     .Select(q => q.Value).Max(), 
     Double.Parse(row.Descendants().Where(y => y.Name.LocalName == valueName) 
          .Select(q => q.Value).Max()) * multiplier); 

<Table> 
    <Year>1</Year> 
    <Statistic>0 Percentile</Statistic> 
    <Return_Value>0.0535644000000</Return_Value> 
</Table> 
    base {System.Xml.Linq.XContainer}: <Table> 
    <ALMRunResults_Year>1</ALMRunResults_Year> 
    <Statistic>0 Percentile</Statistic> 
    <Return_Value>0.0535644000000</Return_Value> 
</Table> 

FirstAttribute: null 
    HasAttributes: false 
    HasElements: true 
    IsEmpty: false 
    LastAttribute: null 
    Name: {Table} 
    NodeType: Element 
    Value: "10 Percentile0.0535644000000" 

编辑

在下面的语句 - 我想最好要。凡两次过滤掉那些没有1个百分。

myHoldingsDistribution.Add(row.Descendants() 
     .Where(y => y.Name.LocalName == keyName) 
     .Select(q => q.Value).Max(), 
     Double.Parse(row.Descendants().Where(y => y.Name.LocalName == valueName) 
          .Select(q => q.Value).Max()) * multiplier); 
+0

你是什么意思“你不能有两个where子句?你从你提供的代码中得到什么错误? – StriplingWarrior 2010-12-07 17:19:08

+0

“我想最好要。凡两次过滤掉那些没有1个百分。” - 什么阻止你?你通常可以只连锁。凡陈述在一起:`rows.Where(...),其中(...)`什么,当你试试这个,什么做的代码看起来像发生? – StriplingWarrior 2010-12-07 17:53:05

回答

2

我真的非常贵LINQ查询糊涂了,好像你在第一段中描述的行为并没有真正反映你的代码试图做的事。我相信你和你的代码所遇到的问题更多的是比什么都重要的Double.Parse方法的位置,因为下面的代码编译和运行得很好:

var myHoldingsDistribution = new Dictionary<object, object>(); 
var rows = new[] {new {Descendants = new[]{new {Name = new {LocalName = "test"}, Value = 0.05}}}}; 
var keyName = "test"; 
var valueName = "test"; 
var multiplier = 2d; 
foreach (var row in rows) 
{ 
    myHoldingsDistribution.Add(row.Descendants 
     .Where(y => y.Name.LocalName == keyName) 
     .Select(q => q.Value).Max(), 
     row.Descendants.Where(y => y.Name.LocalName == valueName) 
         .Select(q => q.Value).Max() * multiplier); 

} 

编辑

我还是很困惑这个问题。到目前为止,你已经给了以下要求:

  1. 有2个,其中在LINQ XML语句条款
  2. 有两个where子句中的一个附加声明的字典对象
  3. SELECT * FROM我的数据集
  4. 筛选出那些不是1百分比

我会忽略前两个因为我不认为他们是必要的。第三个要求比较简单,假设你的数据格式有些固定。 (难道你真的是有一个“年”标签和一个“ALMRunResults_Year”标签?)

// Move the data into an object that can easily be manipulated. 
var transform = from r in rows 
       select new 
       { 
        key = r.Element(keyName).Value, 
        value = Double.Parse(r.Element(valueName).Value), 
        statistic = r.Element(statisticName).Value 
       }; 

一旦你选择了值超出这个样子,你可以使用LINQ到对象来执行你的过滤器描述和创建结果集合字典通过调用ToDictionary

var myHoldingsDistribution = transform 
    // Filter out anybody that's not 1 percentile 
    .Where(r => r.statistic == statisticFilter) 
    // Create a dictionary out of the keys and values 
    .ToDictionary(r => r.key, r => r.value); 

两个代码块,我给你编译和运行就好给出以下设置:

var text = 
@"<Root> 
    <Table> 
     <ALMRunResults_Year>1</ALMRunResults_Year> 
     <Statistic>0 Percentile</Statistic> 
     <Return_Value>0.0535644000000</Return_Value> 
    </Table> 
    <Table> 
     <ALMRunResults_Year>2</ALMRunResults_Year> 
     <Statistic>1 Percentile</Statistic> 
     <Return_Value>0.0535644000000</Return_Value> 
    </Table> 
</Root>"; 
var doc = XDocument.Parse(text); 
var rows = doc.Descendants("Table"); 

var keyName = "ALMRunResults_Year"; 
var valueName = "Return_Value"; 
var statisticName = "Statistic"; 
var statisticFilter = "1 Percentile"; 

这有道理吗?

编辑2

WHERE统计仅= “0个百分点, 25个百分点,50个百分点,75 百分” 等

所以,当你在谈论2 where子句,你的意思是你想要“百分位数= 0或百分位数= 25”,例如?有几种方法可以做到这一点。最简单的方法可能是使用Contains方法:

var statisticFilters = new[]{ 
    "0 Percentile", "25 Percentile", "50 Percentile", "75 Percentile" 
}; 
... 
var myHoldingsDistribution = transform 
    .Where(r => statisticFilters.Contains(r.statistic)) 
    .ToDictionary(r => r.key, r => r.value);