【发布时间】:2014-07-03 17:40:53
【问题描述】:
我有一个类型的字典,
Dictionary<string, string> newdictionary = new Dictionary<string, string>();
newdictionary.Add("12345", "chip1");
newdictionary.Add("23456", "chip2");
现在我有一个类型为
的列表 internal class CustomSerial
{
public string SerialNo { get; set; }
public decimal ecoID { get; set; }
}
var customList = new List<CustomSerial>();
CustomSerial custObj1= new CustomSerial();
custObj1.ecoID =1;
custObj1.SerialNo = "12345";
customList.Add(custObj1);
CustomSerial custObj2 = new CustomSerial();
custObj2.ecoID = 2;
custObj2.SerialNo = "23456";
customList.Add(custObj2);
现在我需要通过使用序列号过滤键并用 ecoID 替换值来更新初始字典。
当我尝试这个时,它给出了
foreach (KeyValuePair<string, string> each in newdictionary)
{
each.Value = customList.Where(t => t.SerialNo == each.Key).Select(t => t.ecoID).ToString();
}
System.Collections.Generic.KeyValuePair.Value' 不能被赋值——它是只读的
【问题讨论】:
-
serials是什么?除此之外LIN(Q)是一个查询不更新的工具。 -
@TimSchmelter 是的新词典
-
foreach 用于从集合中检索数据你知道吗?
-
@DhavalPatel 是的,我知道,但是在获得太有用的密钥后尝试替换,忽略了这一点,有没有办法检查密钥和更新值?
-
@Sajeetharan 您最后一行代码中的
ToString()不太可能按照您的意愿行事。
标签: c# linq dictionary