我有一个数据库,我必须通过odbc连接。如何优化数据表到集合转换的运行时?
数据获取需要应用程序。 2分钟。并且生成的DataTable有350000条记录。
我想把数据表转换成这个对象图。结果集没有主键,主键通过从中获取数据的视图指定。
public class PriceCurve
{
public PriceCurve(DataTable dt)
{
this.Id = int.Parse(dt.AsEnumerable().First()["ID"].ToString());
this.Prices = new List<Price>();
GetPrices(dt);
}
public int Id { get; private set; }
public IList<Price> Prices { get; set; }
private void GetPrices(DataTable dt)
{
foreach (DataColumn column in dt.Columns)
{
switch (this.GetPriceProviderType(column)) // parses ColumnName to Enum
{
case Price.PriceProvider.A:
{
this.Prices.Add(new Price(Price.PriceProvider.A, dt.AsEnumerable()));
}
break;
case Price.PriceProvider.B:
{
this.Prices.Add(new Price(Price.PriceProvider.B, dt.AsEnumerable()));
}
break;
}
}
public class Price
{
public enum PriceProvider
{
A, B
}
public Price(PriceProvider type, IEnumerable<DataRow> dt)
{
this.Type = type;
this.TradingDates = new List<TradingDate>();
this.GetTradingDates(type, dt);
}
public IList<TradingDate> TradingDates { get; set; }
public PriceProvider Type { get; set; }
private void GetTradingDates(PriceProvider type, IEnumerable<DataRow> dt)
{
var data = dt.Select(column => column["TRADING_DATE"]).Distinct();
foreach (var date in data)
{
this.TradingDates.Add(new TradingDate(date.ToString(), type, dt));
}
}
public class TradingDate
{
public TradingDate(string id, PriceProvider type, IEnumerable<DataRow> dt)
{
this.Id = id;
this.DeliveryPeriodValues = new Dictionary<int, double?>();
this.GetDeliveryPeriodValues(type, dt);
}
public string Id { get; set; }
public IDictionary<int, double?> DeliveryPeriodValues { get; set; }
private void GetDeliveryPeriodValues(PriceProvider type, IEnumerable<DataRow> dt)
{
foreach (var row in dt.Where(column => column["TRADING_DATE"].ToString() == this.Name))
{
try
{
this.DeliveryPeriodValues.Add(
int.Parse(row["DELIVERY_PERIOD"].ToString()),
double.Parse(row[Enum.GetName(typeof(Price.PriceProvider), type)].ToString()));
}
catch (FormatException e)
{
this.DeliveryPeriodValues.Add(
int.Parse(row["DELIVERY_PERIOD"].ToString()),
null);
}
}
}
}
}
我创建了一个对象,其中包含一个包含两个对象的列表。这两个对象都包含一个包含1000个对象的列表。这1000个对象中的每一个都包含一个包含350对的字典。
它在调试期间崩溃visual studio 2010,由于OutOfMemory失败或需要几分钟(不可接受)执行。
这个问题的最佳解决方法是什么?我是新来的C#和不知道如何优化通过这个巨大的数据或我的对象图循环。任何帮助表示赞赏。
因此,我应该基本上采取数据读取器,并直接与reader.GetNames(新对象[])返回的每个对象[]) – mrt181 2011-04-19 20:02:06
不,这是一个数组,而不是adata传输对象。你应该立即将它们转换成真正的物品。你知道,具有变量,属性的类型。 – TomTom 2011-04-20 03:53:23
是的,我听说过这个东西;)。 所以最好的方法就像{var neededCol = reader.GetOrdinal(“TRADING_DATE”); var myObj = new PriceCurve(); while(reader.Read()){myObj.MyProperty = reader.GetValue(neededCol);}} – mrt181 2011-04-20 06:46:32