Linq query to get data from array of objects c#
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 :)
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..
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>
.
精彩评论