I'm having difficulty initializing a DataTable
specifying the Columns
and PrimaryKey
with an object initializer:
private DataTable _products = new DataTable
{
Columns = { { "Product", typeof(string) }, { "Lot", typeof(string) }, { "Qty", typeof(int) } },
PrimaryKey = Columns[0] //Columns doens't exist in the current context
};
Is there a way to make it work?
No, you cannot use object initializer syntax if you want to use an object in it which is also initialized in it. But that wouldn't make much sense either.
Instead use the constructor since that is the appropriate place:
private DataTable _products;
public void ClassName()
{
_products = new DataTable
{
Columns = { { "Product", typeof(string) }, { "Lot", typeof(string) }, { "Qty", typeof(int) } }
};
_products.PrimaryKey = new[] { _products.Columns[0] };
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다