Convert Object Hierachey to Object Array
Posted
by
Killercam
on Stack Overflow
See other posts from Stack Overflow
or by Killercam
Published on 2012-12-19T22:45:57Z
Indexed on
2012/12/19
23:03 UTC
Read the original article
Hit count: 161
c#
All, I want to create an object array foo[]
, where the constructor for Foo
is
public Foo(string name, string discription){}
I have a database object which has a structure (not incuding stored procedures, functions or views for simplicity) like
public class Database
{
public string name { get; set; }
public string filename { get; set; }
public List<Table> tables { get; set; }
public Database(string name, string filename)
{
this.name = name;
this.filename = filename;
}
}
protected internal class Table
{
public string name { get; set; }
public List<Column> columns { get; set;}
public Table(string name, List<Column> columns)
{
this.name = name;
this.columns = columns;
}
}
protected internal class Column
{
public string name { get; set; }
public string type { get; set; }
public Column(string name, string type, int maxLength,
bool isNullable)
{
this.name = name;
this.type = type;
}
}
I would like to know the quickest way to add Column
and Table
information to the Foo[]
object array?
Clearly I can do
List<Foo> fooList = new List<Foo>();
foreach (Table t in database.tables)
{
fooList.Add(new Foo(t.Name, "Some Description"));
foreach (Column c in t.columns)
fooList.Add(new Foo(c.Name, "Some Description"));
}
Foo[] fooArr = fooList.ToArray<Foo>();
But is there a quicker way? Clearly LINQ is likely to be slower for a query that does a simalar operation, but I care allot about speed here so any advice would be appreciated. Perhaps the use of a HashSet would be the way to go as there will not be duplicate entries...
Thanks for your time.
© Stack Overflow or respective owner