I have a DataTable that returns me data as follows:
APP | VERSION
____|________
app1| 1.0
app1| 2.0
app1| 3.0
app2| 1.0
app3|
As you see, "app1" has multiple versions, and some apps don't have any versions. I need to store this Datatable result in some sort of List for later usage in my code, so I came up with Dictionary, but can't get It to work. Here is what I tried:
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;
}
However, I get error
"Cannot convert lambda expression to type 'string' because it is not a delegate type".
How could I store my DataTable to dictionary ?
EDIT:
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);