简体   繁体   English

有没有一种方法可以组合2个LINQ2XML查询?

[英]Is there a way to combine 2 LINQ2XML queries?

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)
};

I think something like this should work. 我认为这样的事情应该起作用。 I added the Select method to return the anonymous class. 我添加了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();

If you don't want to use the Select Extension method, you can use LINQ syntax. 如果不想使用“选择扩展名”方法,则可以使用LINQ语法。 Here is an example of this. 这是一个例子。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM