嘿,我在嘗試將 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 } }
};
如您所見,我有一個字典,其鍵是字串,然后對于它的值,我有一個具有 4 個int值的List。
我已經看到 了一些與我上面類似的 例子,但我似乎無法從串列部分中獲得我想要的值。
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 串列中的第二個而不更新所有這些值。
任何幫助,將不勝感激!
更新
我能夠通過遵循 Snales 的建議得到它:
sizeOfPhotoBoxes.Where(w => w.Key == "box2").ToList().ForEach(i => i.Value[2] = 351);
uj5u.com熱心網友回復:
_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")))
...
uj5u.com熱心網友回復:
能夠通過以下無回圈更新:
sizeOfPhotoBoxes["box2"][1] = 4;
uj5u.com熱心網友回復:
給你!
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();
在第一個示例中,您試圖更改鍵,而不是值。
不確定第二個,但這里有一個例子。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/411339.html
標籤:
