【问题标题】:c# updating a value inside a List<int> thats inside a dictionary<string, List<int>> using Lambdac# 使用 Lambda 更新字典<string, List<int>> 内的 List<int> 内的值
【发布时间】:2022-01-14 21:40:44
【问题描述】:

嘿,我在尝试将 Lambda 与我的变量一起使用时遇到了问题,仅仅是因为它不是 Key, value 类型的设置。这意味着我没有典型的Dictionary&lt;string, int&gt;。我有一个Dictionary&lt;string, list&lt;int&gt;&gt;) 类型的设置。

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


    【解决方案1】:

    _datastring, List&lt;int&gt; 的元组。因此,您想使用_data.Value[2] 来访问元组内部的List&lt;int&gt;Key 代表"box**" 部分。

    var 更改为实际类型可能更清楚:

    ...
    foreach (KeyValuePair<string, List<int>> _data in sizeOfPhotoBoxes.Where(w => w.Value.Equals("box2")))
    ...
    

    【讨论】:

    • 按照您的示例,我能够在我的操作中提出更新。谢谢@Snake!
    【解决方案2】:

    能够通过以下无循环更新:

    sizeOfPhotoBoxes["box2"][1] = 4;
    

    【讨论】:

    • “第二名”是 [1]
    • 你说的键值对索引,其中键是 box2 将列表的第二个索引设置为 4
    • 感谢@CaiusJard 的帮助。为了澄清,我说 2 因为它是一个从零开始的列表数组,所以它是 { 0, 1, 2, 3 },其中 2 的值是我需要更改的值。在我给出的示例中,该值为 4。
    【解决方案3】:

    你去!

    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();
    

    在第一个示例中,您尝试更改键,而不是值。

    不确定第二个,但这里有一个例子。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-23
      • 2015-01-14
      • 1970-01-01
      • 1970-01-01
      • 2017-03-11
      相关资源
      最近更新 更多