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

发布于 2024-10-27 11:00:06 字数 1519 浏览 2 评论 0原文

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

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

Name        Type                 ParentType
-------------------------------------------------------
composite   CompositeType        
isThisTest  boolean              
BoolValue   boolean              CompositeType
StringValue string               CompositeType
AnotherType AnotherCompositeType CompositeType
account     string               AnotherCompositeType
startdate   date                 AnotherCompositeType

我想读取此值来构建 xml。类似:

<composite>
    <BoolValue>boolean</BoolValue>
    <StringValue>string</StringValue>
    <AnotherType>
        <account>string</account>
        <startdate>date</startdate>
    </AnotherType>    
<composite>
<isThisTest>boolean</isThisTest>

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

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

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

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

Following values are stored in the array of Parameter:

Name        Type                 ParentType
-------------------------------------------------------
composite   CompositeType        
isThisTest  boolean              
BoolValue   boolean              CompositeType
StringValue string               CompositeType
AnotherType AnotherCompositeType CompositeType
account     string               AnotherCompositeType
startdate   date                 AnotherCompositeType

I want to read this to build an xml. something like:

<composite>
    <BoolValue>boolean</BoolValue>
    <StringValue>string</StringValue>
    <AnotherType>
        <account>string</account>
        <startdate>date</startdate>
    </AnotherType>    
<composite>
<isThisTest>boolean</isThisTest>

I am using the following logic to read the values:

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

Is there a simpler way to read the array to get the parents and thier child? May be using LINQ? I still on .Net 3.5. Appreciate any suggestions with example code :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盛夏尉蓝 2024-11-03 11:00:06

您可以编写一些递归方法来处理此问题:

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

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

请注意,这确实假设数据格式正确。如果两个参数彼此作为父参数,您将得到堆栈溢出。

神奇之处在于 XElements 类 (Linq to 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 );
    }

}

输出:

<composite>
  <BoolValue>boolean</BoolValue>
  <StringValue>string</StringValue>
  <AnotherType>
    <account>string</account>
    <startdate>date</startdate>
  </AnotherType>
</composite> 
<isThisTest>boolean</isThisTest>

编辑

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

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

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

我确信如果确实需要的话,您可能可以调整解决方案以使用 StringBuilder,尽管它可能不会那么优雅。

You could write a little recursive method to deal with this :

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

Each call builds up an Enumerable of XElements which contains the parameters whose parent has the passed in type. The select recurses into the method again finding the children for each Element.

Note that this does assume that the data is correctly formed. If two parameters has eachother as a parent you will get a Stack Overflow.

The magic is in the XElements class (Linq to Xml) that accepts enumerables of XElements to build up the tree like Xml structure.

The first call, pass null (or use default parameters if using C# 4) as the rootType. Use like :

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

}

Output :

<composite>
  <BoolValue>boolean</BoolValue>
  <StringValue>string</StringValue>
  <AnotherType>
    <account>string</account>
    <startdate>date</startdate>
  </AnotherType>
</composite> 
<isThisTest>boolean</isThisTest>

Edit

In response to your comment, XElement gives you two options for outputting as a string.

ToString() will output formatted Xml.

ToString(SaveOptions) allows you to specify formatted or unformatted output as well as ommitting duplicate namespaces.

I'm sure you could probably adapt the solution to use StringBuilder if you really had to, although it probably wouldn't be as elegant..

夏天碎花小短裙 2024-11-03 11:00:06

看起来您想使用递归方法,例如:

string GetChildren(Parameter param, string indent)
{
    StringBuilder sb = new StringBuilder();
    if (HasChildren(param))
    {
        sb.AppendFormat("{0}<{1}>{2}", indent, param.Name, Environment.NewLine);
        foreach (Parameter child in parameters.Where(p => p.ParentType == param.Type))
        {
            sb.Append(GetChildren(child, indent + "   "));
        }
        sb.AppendFormat("{0}</{1}>{2}", indent, param.Name, Environment.NewLine);
    }
    else
    {
        sb.AppendFormat("{0}<{1}>{2}</{1}>{3}", indent, param.Name, param.Type, Environment.NewLine);
    }
    return sb.ToString();
}

查看 Parameter 是否具有子节点的方法如下所示:

bool HasChildren(Parameter param)
{
    return parameters.Any(p => p.ParentType == param.Type);
}

集合 parameters 可以定义为 IEnumerableIEnumerable。 Parameter>并且可以使用List来实现。

It looks like you want to use a recursive method, something like:

string GetChildren(Parameter param, string indent)
{
    StringBuilder sb = new StringBuilder();
    if (HasChildren(param))
    {
        sb.AppendFormat("{0}<{1}>{2}", indent, param.Name, Environment.NewLine);
        foreach (Parameter child in parameters.Where(p => p.ParentType == param.Type))
        {
            sb.Append(GetChildren(child, indent + "   "));
        }
        sb.AppendFormat("{0}</{1}>{2}", indent, param.Name, Environment.NewLine);
    }
    else
    {
        sb.AppendFormat("{0}<{1}>{2}</{1}>{3}", indent, param.Name, param.Type, Environment.NewLine);
    }
    return sb.ToString();
}

The method that looks to see whether a Parameter has child nodes would look like:

bool HasChildren(Parameter param)
{
    return parameters.Any(p => p.ParentType == param.Type);
}

The collection parameters could be defined as an IEnumerable<Parameter> and could be implemented using a List<Parameter>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文