Linq查询从对象数组中获取数据c#

Sri*_*ddy 1 c# linq arrays struct

public struct Parameter 
{
    public Parameter(string name, string type, string parenttype)  
    {        
        this.Name = name;
        this.Type = type;
        this.ParentType = parenttype;        
    }
    public string Name;
    public string Type;
    public string ParentType;
}
Run Code Online (Sandbox Code Playgroud)

以下值存储在参数数组中:

Name        Type                 ParentType
-------------------------------------------------------
composite   CompositeType        
isThisTest  boolean              
BoolValue   boolean              CompositeType
StringValue string               CompositeType
AnotherType AnotherCompositeType CompositeType
account     string               AnotherCompositeType
startdate   date                 AnotherCompositeType
Run Code Online (Sandbox Code Playgroud)

我想读这个来构建一个xml.就像是:

<composite>
    <BoolValue>boolean</BoolValue>
    <StringValue>string</StringValue>
    <AnotherType>
        <account>string</account>
        <startdate>date</startdate>
    </AnotherType>    
<composite>
<isThisTest>boolean</isThisTest>
Run Code Online (Sandbox Code Playgroud)

我使用以下逻辑来读取值:

foreach (Parameter parameter in parameters)
{
    sb.Append("        <" + parameter.Name + ">");
    //HERE: need to check parenttype and get all it's child elements
    //
    sb.Append("</" + parameter.Name + ">" + CrLf);
}
Run Code Online (Sandbox Code Playgroud)

是否有更简单的方法来读取数组以获得父母和他们的孩子?可能正在使用LINQ?我还在.Net 3.5上.通过示例代码感谢任何建议:)

Mon*_*ong 5

你可以编写一个小的递归方法来处理这个问题:

IEnumerable<XElement> GetChildren ( string rootType, List<Parameter> parameters )
{
    return from p in parameters
        where p.ParentType == rootType
        let children = GetChildren ( p.Type, parameters )
        select  children.Count() == 0 ? 
            new XElement ( p.Name, p.Type ) :
            new XElement ( p.Name, children );
}
Run Code Online (Sandbox Code Playgroud)

每次调用都会构建一个Enumerable of XElements,其中包含父元素具有传入类型的参数.select再次递归到方法中,再次找到每个Element的子节点.

请注意,这确实假设数据已正确形成.如果两个参数相互作为父项,则会出现堆栈溢出.

神奇的是在XElements类(Linq到Xml)中,它接受XElements的可枚举来构建像Xml结构一样的树.

第一次调用,传递null(或使用默认参数,如果使用C#4)作为rootType.使用如下:

void Main()
{
    var parameters = new List<Parameter> {
        new Parameter {Name = "composite", Type = "CompositeType" },
        new Parameter {Name = "isThisTest", Type = "boolean" },
        new Parameter {Name = "BoolValue", Type = "boolean", ParentType = "CompositeType" },
        new Parameter {Name = "StringValue", Type = "string", ParentType = "CompositeType" },
        new Parameter {Name = "AnotherType", Type = "AnotherCompositeType", ParentType = "CompositeType" },
        new Parameter {Name = "account", Type = "string", ParentType = "AnotherCompositeType" },
        new Parameter {Name = "startdate", Type = "date", ParentType = "AnotherCompositeType" }
    };

    foreach ( var r in GetChildren ( null, parameters ) )
    {
        Console.WriteLine ( r );
    }

}
Run Code Online (Sandbox Code Playgroud)

输出:

<composite>
  <BoolValue>boolean</BoolValue>
  <StringValue>string</StringValue>
  <AnotherType>
    <account>string</account>
    <startdate>date</startdate>
  </AnotherType>
</composite> 
<isThisTest>boolean</isThisTest>
Run Code Online (Sandbox Code Playgroud)

编辑

为了回应您的评论,XElement为您提供了两个输出字符串的选项.

ToString()将输出格式化的Xml.

ToString(SaveOptions)允许您指定格式化或未格式化的输出以及省略重复的命名空间.

我敢肯定,如果你真的不得不调整解决方案使用StringBuilder,虽然它可能不会那么优雅..