Interactive Brokers API DataTable 覆盖行
Interactive Brokers API DataTable overwrites rows
我正在使用 Interactive Brokers C# API 并尝试从 AccountSummary
对象创建数据 table 但 table 中的行被覆盖并且仅显示最后一个值。
我不知道发生了什么。我认为可能是 属性 类型与列名匹配,因此它覆盖在同一行的顶部。
预期输出
public static DataTable ConverToDataTable(object obj)
{
DataTable dt = new DataTable();
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
obj.GetType().GetProperties().ToList().ForEach(f =>
{
try
{
f.GetValue(obj, null);
dt.Columns.Add(f.Name, f.PropertyType);
dt.Rows[0][f.Name] = f.GetValue(obj, null);
}
catch
{
}
});
return dt;
}
这是调用 ConvertToDataTable() 方法的方法。
public void UpdateUI(IBMessage message)
{
switch (message.Type)
{
case MessageType.AccountSummary:
ConverToDataTable((AccountSummaryMessage)message);
break;
case MessageType.AccountSummaryEnd:
HandleAccountSummaryEnd();
break;
case MessageType.AccountValue:
HandleAccountValue((AccountValueMessage)message);
break;
case MessageType.PortfolioValue:
HandlePortfolioValue((UpdatePortfolioMessage)message);
break;
case MessageType.AccountDownloadEnd:
break;
case MessageType.Position:
HandlePosition((PositionMessage)message);
break;
case MessageType.PositionEnd:
break;
}
}
这是我试图从中构建数据table的对象。
public class AccountSummaryMessage : IBMessage
{
private int requestId;
private string account;
private string tag;
private string value;
private string currency;
public int RequestId
{
get { return requestId; }
set { requestId = value; }
}
public string Account
{
get { return account; }
set { account = value; }
}
public string Tag
{
get { return tag; }
set { tag = value; }
}
public string Value
{
get { return this.value; }
set { this.value = value; }
}
public string Currency
{
get { return currency; }
set { currency = value; }
}
public AccountSummaryMessage(int requestId, string account, string tag, string value, string currency)
{
Type = MessageType.AccountSummary;
RequestId = requestId;
Account = account;
Tag = tag;
Value = value;
Currency = currency;
}
}
您应该先遍历并将列添加到 table,然后再添加行。如果您只是想在 ConverToDataTable
函数中创建一个只有一行的 table,您需要将其更改为类似于此的内容:
public static DataTable ConverToDataTable(object obj)
{
DataTable dt = new DataTable();
var dataType = obj.GetType();
dataType.GetProperties().ToList().ForEach(f =>
{
try
{
dt.Columns.Add(f.Name, f.PropertyType);
}
catch
{
}
});
DataRow newRow = dt.NewRow();
foreach (var prop in dataType.GetProperties())
{
newRow[prop.Name] = prop.GetValue(obj);
}
dt.Rows.Add(newRow);
return dt;
}
编辑:要添加多行,您有多种选择(正如我在下面的评论中所解释的那样。这里有两种可能的方法:
在 class 中使用私有 table 变量以在需要时继续追加行:
private DataDate table = null;
public void ConverToDataTable(object obj)
{
var dataType = obj.GetType();
if (table == null)
{
table = new DataTable();
dataType.GetProperties().ToList().ForEach(f =>
{
table.Columns.Add(f.Name, f.PropertyType);
});
}
DataRow newRow = table.NewRow();
foreach (var prop in dataType.GetProperties())
{
newRow[prop.Name] = prop.GetValue(obj);
}
table.Rows.Add(newRow);
}
使用可枚举类型一次添加所有行:
public static DataTable ConverToDataTable(IEnumerable<object> obj)
{
DataTable dt = new DataTable();
var dataType = obj.First().GetType();
dataType.GetProperties().ToList().ForEach(f =>
{
try
{
dt.Columns.Add(f.Name, f.PropertyType);
}
catch
{
}
});
foreach (var item in obj)
{
DataRow newRow = dt.NewRow();
foreach (var prop in dataType.GetProperties())
{
newRow[prop.Name] = prop.GetValue(item);
}
dt.Rows.Add(newRow);
}
return dt;
}
阅读可枚举类型可能会有帮助。 Here 是一个很好的起点。
我使用了 Kevin 的第一个编辑器,它完美无缺。
我正在使用 Interactive Brokers C# API 并尝试从 AccountSummary
对象创建数据 table 但 table 中的行被覆盖并且仅显示最后一个值。
我不知道发生了什么。我认为可能是 属性 类型与列名匹配,因此它覆盖在同一行的顶部。
预期输出
public static DataTable ConverToDataTable(object obj)
{
DataTable dt = new DataTable();
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
obj.GetType().GetProperties().ToList().ForEach(f =>
{
try
{
f.GetValue(obj, null);
dt.Columns.Add(f.Name, f.PropertyType);
dt.Rows[0][f.Name] = f.GetValue(obj, null);
}
catch
{
}
});
return dt;
}
这是调用 ConvertToDataTable() 方法的方法。
public void UpdateUI(IBMessage message)
{
switch (message.Type)
{
case MessageType.AccountSummary:
ConverToDataTable((AccountSummaryMessage)message);
break;
case MessageType.AccountSummaryEnd:
HandleAccountSummaryEnd();
break;
case MessageType.AccountValue:
HandleAccountValue((AccountValueMessage)message);
break;
case MessageType.PortfolioValue:
HandlePortfolioValue((UpdatePortfolioMessage)message);
break;
case MessageType.AccountDownloadEnd:
break;
case MessageType.Position:
HandlePosition((PositionMessage)message);
break;
case MessageType.PositionEnd:
break;
}
}
这是我试图从中构建数据table的对象。
public class AccountSummaryMessage : IBMessage
{
private int requestId;
private string account;
private string tag;
private string value;
private string currency;
public int RequestId
{
get { return requestId; }
set { requestId = value; }
}
public string Account
{
get { return account; }
set { account = value; }
}
public string Tag
{
get { return tag; }
set { tag = value; }
}
public string Value
{
get { return this.value; }
set { this.value = value; }
}
public string Currency
{
get { return currency; }
set { currency = value; }
}
public AccountSummaryMessage(int requestId, string account, string tag, string value, string currency)
{
Type = MessageType.AccountSummary;
RequestId = requestId;
Account = account;
Tag = tag;
Value = value;
Currency = currency;
}
}
您应该先遍历并将列添加到 table,然后再添加行。如果您只是想在 ConverToDataTable
函数中创建一个只有一行的 table,您需要将其更改为类似于此的内容:
public static DataTable ConverToDataTable(object obj)
{
DataTable dt = new DataTable();
var dataType = obj.GetType();
dataType.GetProperties().ToList().ForEach(f =>
{
try
{
dt.Columns.Add(f.Name, f.PropertyType);
}
catch
{
}
});
DataRow newRow = dt.NewRow();
foreach (var prop in dataType.GetProperties())
{
newRow[prop.Name] = prop.GetValue(obj);
}
dt.Rows.Add(newRow);
return dt;
}
编辑:要添加多行,您有多种选择(正如我在下面的评论中所解释的那样。这里有两种可能的方法:
在 class 中使用私有 table 变量以在需要时继续追加行:
private DataDate table = null;
public void ConverToDataTable(object obj)
{
var dataType = obj.GetType();
if (table == null)
{
table = new DataTable();
dataType.GetProperties().ToList().ForEach(f =>
{
table.Columns.Add(f.Name, f.PropertyType);
});
}
DataRow newRow = table.NewRow();
foreach (var prop in dataType.GetProperties())
{
newRow[prop.Name] = prop.GetValue(obj);
}
table.Rows.Add(newRow);
}
使用可枚举类型一次添加所有行:
public static DataTable ConverToDataTable(IEnumerable<object> obj)
{
DataTable dt = new DataTable();
var dataType = obj.First().GetType();
dataType.GetProperties().ToList().ForEach(f =>
{
try
{
dt.Columns.Add(f.Name, f.PropertyType);
}
catch
{
}
});
foreach (var item in obj)
{
DataRow newRow = dt.NewRow();
foreach (var prop in dataType.GetProperties())
{
newRow[prop.Name] = prop.GetValue(item);
}
dt.Rows.Add(newRow);
}
return dt;
}
阅读可枚举类型可能会有帮助。 Here 是一个很好的起点。
我使用了 Kevin 的第一个编辑器,它完美无缺。