C#DataTable to Dictionary有多个值

时间:2018-03-05 13:23:53

标签: c# dictionary datatable

我有一个DataTable,它返回如下数据:

APP | VERSION
____|________  
app1| 1.0
app1| 2.0
app1| 3.0
app2| 1.0
app3|  

如你所见," app1"有多个版本,有些应用程序没有任何版本。我需要将这个Datatable结果存储在某种List中,以便以后在我的代码中使用,所以我想出了Dictionary,但是不能让它工作。这是我试过的:

    public static Dictionary<string, List<string>> AppVer()
        {
         string SQL = "SELECT column1, column2 FROM myTable a LEFT JOIN 
         myTable2 v ON v.ID_FK=a.ID_PK";
         Dictionary<string, List<string>> versions = new Dictionary<string, 
         List<string>>();

         DataTable dt = new DataTable();

            try
            {
                using (var conn = new OracleConnection(conn_string))
                {
                    conn.Open();

                    using (OracleCommand cmd = new OracleCommand(SQL, conn))
                    {
                        using (OracleDataAdapter dad = new OracleDataAdapter(cmd))
                        {
                            dad.Fill(dt);
                        }
                        versions = dt.AsEnumerable().ToDictionary<DataRow, string, List<string>>(row => row[0].ToString(), new List<string>(row=> row[1].ToString()));
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
             return versions;
        }

然而,我收到错误

  

&#34;无法将lambda表达式转换为&#39; string&#39;因为它不是   代表类型&#34;。

我如何将我的DataTable存储到字典?

编辑:

 Dictionary<string, List<Version>> all_versions =  AppVer();

            StringBuilder sb = new StringBuilder();

            foreach (var item in all_versions)
            {
                sb.AppendFormat("{0} - {1}{2}", item.Key, item.Value, Environment.NewLine);
            }

            string result = sb.ToString().TrimEnd();
            MessageBox.Show(result);

2 个答案:

答案 0 :(得分:2)

ILookup<string, string> myLookup = dt.AsEnumerable().ToLookup(row => row[0].ToString(), row => row[1].ToString());

IEnumerable<string> myValues = myLookup["app1"];

// Loop through lookup.
foreach(var group in myLookup)
{
    string key = group.Key;
    IEnumerable<string> myValues = group;

    Console.WriteLine("Group {0} has {1} elements", key, myValues);
}

答案 1 :(得分:1)

您需要通过密钥GroupBy,然后才能在群组中使用ToList。你知道有一个Version - 类你可以使用吗?所以我更喜欢Dictionary<string, List<Version>>

public static Dictionary<string, List<Version>> AppVer()
{
    Dictionary<string, List<Version>> versions = null;
    string SQL = "SELECT column1, column2 FROM myTable a LEFT JOIN myTable2 v ON v.ID_FK = a.ID_PK";
    var dt = new DataTable();

    try
    {
        using (var conn = new OracleConnection(conn_string))
        using (var da = new OracleDataAdapter(SQL, conn))
        {
            da.Fill(dt);
        }

        versions = dt.AsEnumerable()
            .GroupBy(r => r.Field<string>(0))
            .ToDictionary(
                g => g.Key, 
                g => g.Select(r => Version.Parse(r.Field<string>(1))).ToList());
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

    return versions;
}

Version类的一个好处是它正确排序,因此10.1之后2.1