我面臨一個問題,我無法弄清楚如何管理。關於繼承和鑄
我開發了一個在DataTable上實現非唯一索引的類;處理商品,我想發展我IndexedTable
的是,obviousvly,從DataTable
繼承,添加一個方法使用非唯一索引
我寫信給執行表上的搜索:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace DWH_Core
{
class IndexedTable : DataTable
{
//private DataTable m_tbl;
//public Dictionary<Int32, Int32> idx;
private Index_NonUnique m_idx = null;
public void buildNonUniqueIndex(string keyField)
{
m_idx = new Index_NonUnique();
m_idx.buildIndex(this, keyField);
return;
}
public DataRow[] search (string key)
{
return m_idx.search(key);
}
public DataRow[] search(int key)
{
return m_idx.search(key);
}
public DataRow[] search(short key)
{
return m_idx.search(key);
}
}
}
和相關類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace DWH_Core
{
class Index_NonUnique
{
private string m_KeyField;
private ILookup<object, DataRow> m_IdxByKey;
public void buildIndex(DataTable tbl, string keyField)
{
m_KeyField = keyField;
IEnumerable<DataRow> enumerable = tbl.AsEnumerable();
m_IdxByKey = enumerable.ToLookup(o => o[keyField]);
}
public DataRow[] search(string keyValue)
{
return m_IdxByKey[keyValue].ToArray();
}
public DataRow[] search(int keyValue)
{
return m_IdxByKey[keyValue].ToArray();
}
public DataRow[] search(short keyValue)
{
return m_IdxByKey[keyValue].ToArray();
}
}
}
的問題是,當我產生,在通常的方式一個DataTable類,並嘗試將其轉換爲IndexedTable,我得到了運行時錯誤「無法從類型的對象執行投「SYSTE類型'DWH_Core.IndexedTable'中的「m.Data.DataTable」。 我不想使用private DataTable m_tbl
成員,因爲我想將所有DataTable成員應用到我的IndexedTable對象,但我無法理解如何管理此錯誤。 最後,我的目標是編寫這樣的事:
IndexedTable tmpTable = null;
SqlCommand cmd = "SELECT * FROM SOME_TABLE"
SqlDataReader rdr = cmd.ExecuteReader();
m_SAP_Allocazione = (IndexedTable)rdr.ToTable();
m_SAP_Allocazione.buildNonUniqueIndex("PERNR");
謝謝@aleha,我恢復的內部公共件上。 –