c# 使用 Lambda 更新字典 <string, List<int>> 中的 List<int> 中的值
c# updating a value inside a List<int> thats inside a dictionary<string, List<int>> using Lambda
嘿,我在尝试将 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 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 Property or indexer 'string.this[int]' cannot be assigned to -- it is read only
也试过这个产生与上面相同的错误:
var _data = sizeOfPhotoBoxes.Where(w => w.Key == "box2").ToList().ForEach(i => i.Value = 35);
但是那也没用。
我想更新 box2 的列表值,即 4 列表中的第 2nd,而不更新所有框。
如有任何帮助,我们将不胜感激!
更新
我是按照 Snales 的建议得到的:
sizeOfPhotoBoxes.Where(w => w.Key == "box2").ToList().ForEach(i => i.Value[2] = 351);
_data
是 string, List<int>
的元组。因此,您想使用 _data.Value[2]
来访问元组内部的 List<int>
。 Key
代表 "box**"
部分。
将var
改为实际类型可能会更清楚:
...
foreach (KeyValuePair<string, List<int>> _data in sizeOfPhotoBoxes.Where(w => w.Value.Equals("box2")))
...
能够通过以下无循环更新:
sizeOfPhotoBoxes["box2"][1] = 4;
给你!
Dictionary<string, List<int>> boxes = 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 } }
};
var box2 = boxes.Where(box => box.Key.Equals("box2")).FirstOrDefault();
Console.WriteLine(box2.Value[1]);
box2.Value[1] = 400;
Console.WriteLine(box2.Value[1]);
Console.ReadKey();
在第一个示例中,您试图更改密钥,而不是值。
不确定第二个,但这里有一个例子。
嘿,我在尝试将 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
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 Property or indexer 'string.this[int]' cannot be assigned to -- it is read only
也试过这个产生与上面相同的错误:
var _data = sizeOfPhotoBoxes.Where(w => w.Key == "box2").ToList().ForEach(i => i.Value = 35);
但是那也没用。
我想更新 box2 的列表值,即 4 列表中的第 2nd,而不更新所有框。
如有任何帮助,我们将不胜感激!
更新
我是按照 Snales 的建议得到的:
sizeOfPhotoBoxes.Where(w => w.Key == "box2").ToList().ForEach(i => i.Value[2] = 351);
_data
是 string, List<int>
的元组。因此,您想使用 _data.Value[2]
来访问元组内部的 List<int>
。 Key
代表 "box**"
部分。
将var
改为实际类型可能会更清楚:
...
foreach (KeyValuePair<string, List<int>> _data in sizeOfPhotoBoxes.Where(w => w.Value.Equals("box2")))
...
能够通过以下无循环更新:
sizeOfPhotoBoxes["box2"][1] = 4;
给你!
Dictionary<string, List<int>> boxes = 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 } }
};
var box2 = boxes.Where(box => box.Key.Equals("box2")).FirstOrDefault();
Console.WriteLine(box2.Value[1]);
box2.Value[1] = 400;
Console.WriteLine(box2.Value[1]);
Console.ReadKey();
在第一个示例中,您试图更改密钥,而不是值。
不确定第二个,但这里有一个例子。