2011-09-07 63 views
1
var instructions = (from item in config.Elements("import") 
select new 
{ 
    name = item.Attribute("name").Value, 
    watchFolder = item.Attribute("watchFolder").Value, 
    root = item.Element("documentRoot").Value, 
    DocumentNameDynamic = item.Element("documentName").Attribute("xpath").Value, 
    DocumentNameStatic = item.Element("documentName").Attribute("static").Value, 
    TemplateName = item.Element("template").Attribute("template").Value, 
    Path = item.Element("path").Attribute("path").Value, 
    fields = item.Element("fields").Elements() 
}).SingleOrDefault(); 

var fields = from item in instructions.fields 
select new 
{ 
    xpath = item.Attribute("xpath").Value, 
    FieldName = item.Attribute("FieldName").Value, 
    isMultiValue = bool.Parse(item.Attribute("multiValue").Value) 
}; 
+1

您打算如何組合這些查詢?你只需要一個查詢來生成'fields'的內容? 'instructions'? 「說明」和「字段」?你需要'fields'變量的項目在'instructions'內的'fields'屬性內嗎? –

回答

0

我覺得應該這樣。我添加了Select方法來返回匿名類。

var instructions = (from item in config.Elements("import") 
select new 
{ 
    name = item.Attribute("name").Value, 
    watchFolder = item.Attribute("watchFolder").Value, 
    root = item.Element("documentRoot").Value, 
    DocumentNameDynamic = item.Element("documentName").Attribute("xpath").Value, 
    DocumentNameStatic = item.Element("documentName").Attribute("static").Value, 
    TemplateName = item.Element("template").Attribute("template").Value, 
    Path = item.Element("path").Attribute("path").Value, 
    fields = item.Element("fields").Elements().Select(item => new { 
     xpath = item.Attribute("xpath").Value, 
     FieldName = item.Attribute("FieldName").Value, 
     isMultiValue = bool.Parse(item.Attribute("multiValue").Value) 
    } 
).SingleOrDefault(); 

如果您不想使用Select Extension方法,則可以使用LINQ語法。這是一個例子。

var instructions = (from item in config.Elements("import") 
select new 
{ 
    name = item.Attribute("name").Value, 
    watchFolder = item.Attribute("watchFolder").Value, 
    root = item.Element("documentRoot").Value, 
    DocumentNameDynamic = item.Element("documentName").Attribute("xpath").Value, 
    DocumentNameStatic = item.Element("documentName").Attribute("static").Value, 
    TemplateName = item.Element("template").Attribute("template").Value, 
    Path = item.Element("path").Attribute("path").Value, 
    fields = from e in item.Element("fields").Elements() 
      select new { 
       xpath = item.Attribute("xpath").Value, 
       FieldName = item.Attribute("FieldName").Value, 
       isMultiValue = bool.Parse(item.Attribute("multiValue").Value) 
      } // End of inner select statement 
} // End of outer select statement 
).SingleOrDefault(); 
相關問題