【发布时间】:2022-01-14 21:40:44
【问题描述】:
嘿,我在尝试将 Lambda 与我的变量一起使用时遇到了问题,仅仅是因为它不是 Key, value 类型的设置。这意味着我没有典型的Dictionary<string, int>。我有一个Dictionary<string, list<int>>) 类型的设置。
public static Dictionary<string, List<int>> sizeOfPhotoBoxes = new Dictionary<string, List<int>>()
{
{ "box1", new List<int> {357, 272, 8, 5 } },
{ "box2", new List<int> {357, 272, 4, 5 } },
{ "box3", new List<int> {365, 460, 37, 6 } },
{ "box4", new List<int> {365, 265, 8, 6 } },
{ "box5", new List<int> {715, 455, 15, 11 } },
{ "box6", new List<int> {360, 465, 98, 6 } },
{ "box7", new List<int> {360, 465, 44, 6 } },
{ "box8", new List<int> {360, 465, 28, 6 } },
{ "box9", new List<int> {540, 290, 39, 9 } },
{ "box10",new List<int> {540, 290, 10, 9 } }
};
如您所见,我有一个 Dictionary,其键是 string,然后对于它的 value,我有一个 List 具有 4 个 int 值。
我有 seen some examples 类似于我上面的东西,但我似乎无法从列表部分中获得我想要的值。
foreach (var _data in sizeOfPhotoBoxes.Where(w => w.Value.Equals("box2")))
{
_data.Key[2] = 35; //updating the value in the 3rd place in the list
}
我可以得到 值,因为那是 字典的普通键,但在那之后我不知所措。错误是:
CS0200 属性或索引器 'string.this[int]' 无法分配给 -- 它是只读的
也试过这个产生与上述相同的错误:
var _data = sizeOfPhotoBoxes.Where(w => w.Key == "box2").ToList().ForEach(i => i.Value = 35);
但这也没有用。
我想更新 box2 的列表值,即 4 个列表中的 第 2 个,而不更新所有这些值。
任何帮助将不胜感激!
更新
按照 Snales 的建议,我能够得到它:
sizeOfPhotoBoxes.Where(w => w.Key == "box2").ToList().ForEach(i => i.Value[2] = 351);
【问题讨论】:
标签: c# linq dictionary lambda