|
■No40788 (初心者 さん) に返信
重複と聞いてハッシュテーブルが思い浮かびました。
static void Main(string[] args)
{
IDictionary<string, IList<DataRow>> list = getRepetition(getData(), new int[] { 1 });
foreach (string key in list.Keys)
{
IList<DataRow> rows = list[key];
foreach (DataRow row in rows)
{
Console.WriteLine("key:{0} count:{1} id:{2} name:{3} age:{4}",
key, rows.Count, row[0], row[1], row[2]);
}
}
}
static IDictionary<string, IList<DataRow>> getRepetition(DataTable table, int[] keyIndexes)
{
IDictionary<string, IList<DataRow>> dic =
new SortedList<string, IList<DataRow>>();
foreach (DataRow row in table.Rows)
{
string key = getKey(row, keyIndexes);
if (!dic.ContainsKey(key))
{
dic.Add(key, new List<DataRow>());
}
dic[key].Add(row);
}
return dic;
}
static string getKey(DataRow row, int[] indexes)
{
string key = "";
foreach (int index in indexes)
{
key += row[index].ToString();
}
return key;
}
static DataTable getData()
{
DataTable table = new DataTable("Member");
table.Columns.Add(new DataColumn("id", typeof(int)));
table.Columns.Add(new DataColumn("name", typeof(string)));
table.Columns.Add(new DataColumn("age", typeof(int)));
table.Rows.Add(1, "もりお", 30);
table.Rows.Add(2, "もりお", 40);
table.Rows.Add(3, "まゆみ", 30);
return table;
}
|