There is a class that stores the tree of elements. Child elements are stored in
public List<BaseTreeData> Child { get; set; }
I want to display this tree as a "flat" (linear) list of all elements. After the class is divided into two (base and heir), the GetChildren method generates an error about the type mismatch. Most likely everything is logical, but how to fix it?
Error CS1503 Argument 1: cannot convert from 'ConsoleApplication1.BaseTreeData' to 'ConsoleApplication1.TreeData'
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var data = new List<TreeData>();
for (int i = 0; i < 5; i++)
{
var item = new TreeData() { Name = i.ToString() };
for (int j = 0; j < 3; j++)
{
var number = (i + 1) * 10 + j;
item.Child.Add(new TreeData() { ID = number, Name = number.ToString(), Parent = item });
}
data.Add(item);
}
foreach (var item in data.SelectMany(x => GetChildren(x)))
{
Console.WriteLine(item.ID + " " + item.Name + " " + item.IsChecked);
}
}
static IEnumerable<TreeData> GetChildren(TreeData d)
{
return new[] { d }.Concat(d.Child).SelectMany(x => GetChildren(x));
}
}
class BaseTreeData
{
public bool IsChecked { get; set; }
public BaseTreeData Parent { get; set; }
public List<BaseTreeData> Child { get; set; }
public BaseTreeData()
{
Child = new List<BaseTreeData>();
}
}
class TreeData : BaseTreeData
{
public int ID { get; set; }
public string Name { get; set; }
}
}