[英]c# updating a value inside a List<int> thats inside a dictionary<string, List<int>> using Lambda
Hey all I have a issue with trying to use Lambda with my variable simply because its not a Key, value
type of setup.嘿,我在尝试将 Lambda 与我的变量一起使用时遇到问题,仅仅是因为它不是
Key, value
类型的设置。 Meaning I don't have the typical Dictionary<string, int>
.意思是我没有典型的
Dictionary<string, int>
。 I have a Dictionary<string, list<int>>)
-kind of setup.我有一个
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 } }
};
As you can see I have a Dictionary with a key that's a string and then for its value I have a List that has 4 int values.如您所见,我有一个字典,其键是字符串,然后对于它的值,我有一个具有 4 个int值的List 。
I've seen some examples of something similar to mine above but I can not seem to get the value I want from with in the list part.我已经看到了一些与我上面类似的例子,但我似乎无法从列表部分中获得我想要的值。
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
}
I can get the value since that's the normal key of the dictionary but after that I am at a loss.我可以得到这个值,因为那是字典的普通键,但在那之后我不知所措。 And the error is:
错误是:
CS0200 Property or indexer 'string.this[int]' cannot be assigned to -- it is read only
CS0200 无法将属性或索引器“string.this[int]”分配给 -- 它是只读的
Also tried this that produces the same error as the above one:也试过这个产生与上述相同的错误:
var _data = sizeOfPhotoBoxes.Where(w => w.Key == "box2").ToList().ForEach(i => i.Value = 35);
But that too did not work.但这也不起作用。
I am wanting to update box2's list value that is the 2nd in the list of the 4 without updating all of them.我想更新 box2 的列表值,它是 4 列表中的第二个而不更新所有这些值。
Any help would be appreciated!任何帮助,将不胜感激!
UPDATE更新
I was able to get it by following Snales suggestion:我能够通过遵循 Snales 的建议得到它:
sizeOfPhotoBoxes.Where(w => w.Key == "box2").ToList().ForEach(i => i.Value[2] = 351);
_data
is a tuple of string, List<int>
. _data
是string, List<int>
。 Ergo, you want to use _data.Value[2]
to get access to the List<int>
inside of the tuple.因此,您想使用
_data.Value[2]
来访问元组内的List<int>
。 The Key
represents the "box**"
part. Key
代表"box**"
部分。
It might be clearer to change the var
to the actual type:将
var
更改为实际类型可能更清楚:
...
foreach (KeyValuePair<string, List<int>> _data in sizeOfPhotoBoxes.Where(w => w.Value.Equals("box2")))
...
Was able to update via following without loop:能够通过以下无循环更新:
sizeOfPhotoBoxes["box2"][1] = 4;
There you go!给你!
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();
In the first example you were trying to change the Key, not the value.在第一个示例中,您试图更改键,而不是值。
Not sure about the second one, but here you have an example.不确定第二个,但这里有一个例子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.