2016-08-14 133 views
0

我已经从具有与每个行/场为名称/值的对象的阶层数据中的阵列按照此图像的SOAP查询返回一个响应: object hierarchy选择语句

我示出第一个字段'sequnumb',但Fields数组中有很多字段。

现在我可以通过这些字段数组项的循环,做一个巨大的开关/ case语句和“扁平化”的对象变成什么,我需要。 但我想知道是否有更好的方式使用linq。

我想有什么是这样的:

while (response.Records != null) 
{ 
    var rows = from row in response.Records 
        from field in row.Fields 
        where field.Name == "status" && field.Value[0] != 'X' 
        select new { 
         seqnumb = from f in field where f.Name == "seqnumb" select f.Value 
        }; 
    foreach (var row in rows) 
    { 

    } 
    // get new response.. 
} 

如果返回了值seqnumb = 3(从场阵列许多其它参数)的对象。

目前我有'选择行'作为linq语句中的最后一条语句,我用switch/case迭代了一个工作,但有很多代码,我想提高我对linq的理解。

感谢您的阅读。

+0

所以你要填的空块与逻辑你的榜样构建类型T的对象为“response.Records”每个“行”,或者你想在产生类型T的对象的可迭代LINQ表达式来替换你的“行”的宣言?无论哪种方式,你需要使用T. – Peter

+0

构造函数或工厂我想知道如果可能的话如何做到这一切的LINQ。目前我正在做一个foreach(行中的行){foreach(field in row.Fields){}}类型的迭代。 – pgee70

回答

0

不知道如果我完全掌握你的描述或不是,但它听起来好像这样的工作:

var fields = 
from row in response.Records 
where 
    row.Fields.Any(f1 => f1.Name == "status" && f1.Value != 'X') && 
    row.Fields.Any(f2 => f2.Name == "seqnumb" && f2.Value == "3") 
select(r => r.Fields); 

/* 
* fields, if not null, now contains the complete fields collection 
* of the row that you appear to be interested in. 
* 
* You mentioned wanting a row that has a field named seqnumb == "3" 
* and your code also showed that you want to ensure that the fields 
* collection for that row have a field named "status" != 'X' 
* 
* You can pull--from var fields above--a list of only 
* the fields that you are interested in via something like the below 
*/ 

List<string> fieldNamesWanted = new List<string> 
    {"seqnumb", "someotherfield", "anotherfield"}; 

var onlyTheFieldsYouWant = 
(
    from field in fields 
    where fieldNamesWanted.Any(x => x == field.Name) 
    select field 
).ToList();