Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将数据库结果转换为字典<;int,List<;十进制>&燃气轮机;_C#_Linq_List_Dictionary - Fatal编程技术网

C# 如何将数据库结果转换为字典<;int,List<;十进制>&燃气轮机;

C# 如何将数据库结果转换为字典<;int,List<;十进制>&燃气轮机;,c#,linq,list,dictionary,C#,Linq,List,Dictionary,我们有一个返回以下行的表: ID (integer) PRICE (decimal) 106 5.2 106 6.7 107 9.2 107 8.3 107 3.2 我需要将此表中的值转换为如下内容: 106, (5.2, 6.7) 107, (9.2, 8.3, 3.2) 在本例中,这是字典,但不一定是字典。如果必要的话,我可以

我们有一个返回以下行的表:

ID (integer)   PRICE (decimal)
106                 5.2
106                 6.7
107                 9.2
107                 8.3
107                 3.2
我需要将此表中的值转换为如下内容:

106, (5.2, 6.7)
107, (9.2, 8.3, 3.2)
在本例中,这是
字典
,但不一定是字典。如果必要的话,我可以改变它


ID的数量和相应的价格不是固定的(所以我们可以在这个列表中有108个,它可以有一个或多个价格)

以下是我所做的:

var retVal = new Dictionary<int, List<decimal>>();
var dbRow = new List<decimal>();
var itemsPerRow = new List<List<decimal>>();
var retVal=new Dictionary();
var dbRow=新列表();
var itemsPerRow=新列表();

//循环表
使用(var dr=cmd.ExecuteReader())
{
while(dr.Read())
{
Add(int.Parse(dr[“ID”].ToString());
dbRow.Add(decimal.Parse(dr[“PRICE”].ToString());
itemsPerRow.Add(dbRow);
}        
}
//获取不同ID的
var gb=itemsPerRow.GroupBy(x=>x[0])。选择(x=>x.Key);
foreach(变量行,单位为gb)
{
如果(!retVal.ContainsKey((int)行))
{
IEnumerable ratesfrow=itemsPerRow.Where(x=>x.Contains(row))。选择(x=>x[1]);
retVal.Add((int)行,ratesForRow.ToList());
}
}
这是可行的,但它似乎不是很直截了当,我不高兴的是,我正在循环数据库结果,创建一个列表,然后再次循环列表,而且我的列表被声明为包含小数,但ID列是int,所以我必须将其转换为int。这样可以吗,或者有其他方法可以这样做吗


谢谢

让我们看看。首先,您只需要一个列表:

var tempResults = Enumerable.Empty<KeyValuePair<int, decimal>>().ToList();
using (var dr = cmd.ExecuteReader())
while (dr.Read())
     tempResults.Add(new KeyValuePair<int, decimal> 
            { Key = int.Parse(dr["ID"].ToString()), 
              Value = decimal.Parse(dr["PRICE"].ToString()) });

您可以在一个循环中直接执行此操作:

  Dictionary<int, List<Decimal>> result = new Dictionary<int, List<Decimal>>();

  using (var reader = cmd.ExecuteReader()) {
    while (reader.Read()) {
      int id = int.Parse(reader["ID"].ToString());
      Decimal price = Decimal.Parse(reader["PRICE"].ToString());

      List<Decimal> list;

      if (result.TryGetValue(id, out list))    
        list.Add(price);
      else 
        result.Add(id, new List<Decimal> {price});
    }
  }
字典结果=新建字典();
使用(var reader=cmd.ExecuteReader()){
while(reader.Read()){
int id=int.Parse(reader[“id”].ToString());
Decimal price=Decimal.Parse(读卡器[“price”].ToString());
名单;
if(结果TryGetValue(id,输出列表))
列表。添加(价格);
其他的
添加(id,新列表{price});
}
}

但是,当您尝试插入与密钥相同的ID时,这不会给出字典中已经存在的密钥错误吗?“因此,我们可以在此列表中有108个,并且它可以有一个或多个价格)。“这毫无意义,您需要一个规则来区分值。产品如何可以有多个价格,而产品没有差异,你没有办法把他们分开。如果您将其存储在DB中,非唯一整数肯定不是ID,您将创建一个单独的标识ID字段,但如果它们相同,为什么要同时存储两者?您不理解标识的简单原理,标识是唯一的。ID是唯一的,需要一对多的关系,然后只需要一个简单的导航属性。
return tempResults.GroupBy(k => k.Key).ToDictionary(g => g.Key, g => g.ToList());
  Dictionary<int, List<Decimal>> result = new Dictionary<int, List<Decimal>>();

  using (var reader = cmd.ExecuteReader()) {
    while (reader.Read()) {
      int id = int.Parse(reader["ID"].ToString());
      Decimal price = Decimal.Parse(reader["PRICE"].ToString());

      List<Decimal> list;

      if (result.TryGetValue(id, out list))    
        list.Add(price);
      else 
        result.Add(id, new List<Decimal> {price});
    }
  }