CSharp · Console
using System;
using System.Collections.Generic;
using System.Linq;
namespace Sample
{
public static class Extensions
{
public static IEnumerable<T> Flatten<T>(this T source, Func<T, IEnumerable<T>> selector)
{
return selector(source).SelectMany(c => Flatten(c, selector))
.Concat(new[] { source });
}
}
public class ModelType
{
public string ModelTitle;
public long ModelId;
}
public class CodeType
{
public string CodeValue;
}
public class NodeType
{
public string NodeTypeDescription { get; set; }
public object Item { get; set; }
public NodeType[] Node { get; set; }
}
public class Program
{
public static void Main()
{
var NodeTypes = new NodeType[] {
new NodeType{
NodeTypeDescription = "First Level",
Item = new CodeType(){ CodeValue = "01" },
Node = new NodeType[]{
new NodeType{
NodeTypeDescription = "First Child Level 0101",
Item = new CodeType(){ CodeValue = "0101" },
Node = new NodeType[]{
new NodeType{
NodeTypeDescription = "Target Level Description",
Item = new ModelType(){ ModelId = 1234, ModelTitle = "Target-1234" },
Node = null
}
}
},
new NodeType{
NodeTypeDescription = "First Child Level 0102",
Item = new CodeType(){ CodeValue = "0102" },
Node = new NodeType[]{
new NodeType{
NodeTypeDescription = "Target Level Description",
Item = new ModelType(){ ModelId = 2345, ModelTitle = "Target-2345" },
Node = null
}
}
}
}
},
new NodeType{
NodeTypeDescription = "Second Level",
Item = new CodeType(){ CodeValue = "02" },
Node = new NodeType[]{
new NodeType{
NodeTypeDescription = "Second Child Level 0201",
Item = new CodeType(){ CodeValue = "0201" },
Node = new NodeType[]{
new NodeType{
NodeTypeDescription = "Second Child Level 020101",
Item = new CodeType(){ CodeValue = "020101" },
Node = new NodeType[]{
new NodeType{
NodeTypeDescription = "Target Level Description",
Item = new ModelType(){ ModelId = 2345, ModelTitle = "Target-2345" },
Node = null
}
}
}
}
},
new NodeType{
NodeTypeDescription = "Second Child Level 0202",
Item = new CodeType(){ CodeValue = "0202" },
Node = new NodeType[]{
new NodeType{
NodeTypeDescription = "Target Level Description",
Item = new ModelType(){ ModelId = 3456, ModelTitle = "Target-3456" },
Node = null
}
}
}
}
}
};
var result = NodeTypes[0].Flatten(x => x.Node).Where(y => ((ModelType)(y.Item)).ModelId == 2345);
}
}
}