Make the key an object
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DataTable table = new DataTable();
table.Columns.Add("LKP_ID", typeof(int));
table.Columns.Add("LKP_CODE", typeof(string));
table.Columns.Add("LKP_DESC", typeof(string));
table.Rows.Add(new object[] { 1, "FOOD", "Pasta Bake"});
table.Rows.Add(new object[] { 2, "FOOD", "Chicken Tikka"});
table.Rows.Add(new object[] { 3, "FOOD", "Fries"});
table.Rows.Add(new object[] { 4, "FOOD", "Chicken Fillet Burger"});
string colName = "LKP_ID";
Dictionary<object, List<DataRow>> dict1 = GetDictionary(table, colName);
colName = "LKP_CODE";
Dictionary<object, List<DataRow>> dict2 = GetDictionary(table, colName);
}
static Dictionary<object, List<DataRow>> GetDictionary(DataTable table, string colName)
{
Dictionary<object, List<DataRow>> dict = table.AsEnumerable()
.GroupBy(x => x.Field<object>(colName), y => y)
.ToDictionary(x => x.Key, y => y.ToList());
return dict;
}
}
}
jdweng
No comments:
Post a Comment