好,我想创建一个对象数组foo[]
,其中对于Foo
构造是转换对象层次结构,以对象数组
public Foo(string name, string discription){}
我有具有结构的数据库对象(未incuding存储过程,函数或意见为简单起见)像
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;
}
}
我想知道添加Column
和Table
信息到Foo[]
对象数组的最快方法?
显然,我可以做
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>();
但有一个更快的方法?很明显LINQ对于执行simalar操作的查询可能会比较慢,但我在意这里关于速度的所有信息,所以我们将不胜感激。也许使用HashSet将是不错的选择......
感谢您的时间。
考虑从列表<>属性中删除setter。你并不需要它们(移除setter将不会使对象只能读取对象的引用)。 –
是什么让你认为LINQ会变慢? –
最后,为什么你需要一个数组呢?为什么,确切地说,是一个列表不够好?这个问题的答案可能会影响如何解决问题的建议。 –