C# 将查询结果存储在字典中

C# 将查询结果存储在字典中,c#,collections,dictionary,.net-2.0,C#,Collections,Dictionary,.net 2.0,我在使用C开发Windows mobile应用程序时遇到问题,我正在尝试使用3查询。 我想将所有结果查询收集到字典中 我可以不循环地收集它吗 我的代码如下: string query= "select distinct nameProduct, price from product"; SqlCeCommand cmd = new SqlCeCommand(sql, dbConn); Dictionary production = new Dictionary<string, int>

我在使用C开发Windows mobile应用程序时遇到问题,我正在尝试使用3查询。 我想将所有结果查询收集到字典中

我可以不循环地收集它吗

我的代码如下:

string query= "select distinct nameProduct, price from product";
SqlCeCommand cmd = new SqlCeCommand(sql, dbConn);
Dictionary production = new Dictionary<string, int>();
我如何从我的查询设置这个产品,因为我在一个过程中有3个这样的查询,并且数据超过10.000

@约翰·桑德斯:
当我尝试此操作时,会得到错误{行/列不存在任何数据。}但实际上,当我在sql server中尝试此查询时,仍然运行良好。行中的错误:production[stringreader[nameProduct]=intreader[price]

@约翰尼: 不幸的是,这个项目是在VS2005中构建的,不支持union和join语句

Dictionary production = new Dictionary<string, int>();
string query= "select distinct nameProduct, price from product";
using (SqlCeCommand cmd = new SqlCeCommand(sql, dbConn))
{
    using (var reader = cmd.ExecuteReader())
    {
        while (reader.Read())
        {
            production[(string)reader["nameProduct"]] = (int)reader["price"];
        }
    }
}
没有循环。注意:我没有尝试过:

production =
    reader.Cast<IDataRecord>()
          .ToDictionary(
              record => (string)record["nameProduct"], 
              record => (int)record["price"]);

您可以使用DataAdapter.Fill方法将DataTable结果转换为字典

string query= "select distinct nameProduct, price from product";
SqlCeConnection conn = new SqlCeConnection("Data Source = your file.sdf");

SqlCeCommand selectCmd = conn.CreateCommand();
selectCmd.CommandText = query;

SqlCeDataAdapter adp = new SqlCeDataAdapter(selectCmd);
DataTable dt=new DataTable();
adp.Fill(dt);

Dictionary<string, int> result = (from row in dt.AsEnumerable()
                          select new KeyValuePair<string, int>
                          (row.Field<string>("nameProduct"), row.Field<int>("price")))
                          .ToDictionary(p=>p.Key,p=>p.Value);

 foreach (var t in result)
  {
    Console.WriteLine(t.Key + " " + t.Value);
  }
几点

1.我认为您可以在查询中使用union all。
2.如果您有超过10000行的记录,特别是在移动设备上,我建议您只加载您需要的数据,并且您可以在一个屏幕上显示,而不是全部读取。

这是从数据库获取数据并将数据存储在字典中的简单方法

Dictionary<int, string>emp = new Dictionary<int, string>();
OleDbCommand cmd = new OleDbCommand("select empId,Name from Employee where ORDER BY empId DESC", con);
OleDbDataReader dr = cmd.ExecuteReader();

if (dr.HasRows)
{
    while (dr.Read())
    {
         emp.Add(Convert.ToInt32(dr["empId"]), dr["Name"].ToString());                
    }                    
}

foreach (KeyValuePair<int, string> em in emp)
{
    Console.WriteLine(em.Key.ToString() + " = " + em.Value);
}

OP询问他是否可以不使用循环。不使用循环是不可能的,尽管可以将循环隐藏在内部,例如ToDictionary。While是循环,ToDictionary也是。正确的答案是不,你不能这样做。从水果碗里拿3个苹果的唯一方法是走到柜台前,数一数,排除橘子和香蕉,只拿3个苹果。这可能看起来没什么大不了的,因为人类知道3个苹果却不去想它。但计数和排除的过程仍然需要在潜意识层面上进行。你无法避免它,也无法在不经过所有步骤的情况下实现手中的3个苹果。如果这是10万次找出不同的东西。你认为a、11.0 a、12.0你需要指定使用的是哪个版本的.NET。您已经过期五年了,需要这样说。当我尝试此操作时,会出现错误{行/列不存在数据。}但实际上,当我尝试在sql server中执行查询时,仍然运行良好。行中的错误:production[stringreader[nameProduct]=intreader[price];