嘿,我正在嘗試查詢串列中第一個位置的字串編號:
List<string[]> idMainDescriptionIcon = new List<string[]>(){
// [ID] [Main] [Description] "XX[d=day or n=night]"
new string[4] { "200", "Thunderstorm", "thunderstorm with light rain", "11" },
new string[4] { "201", "Thunderstorm", "thunderstorm with rain", "11" },
new string[4] { "202", "Thunderstorm", "thunderstorm with heavy rain", "11" },
new string[4] { "210", "Thunderstorm", "light thunderstorm", "11" },
new string[4] { "211", "Thunderstorm", "thunderstorm", "11" }
};
我正在使用的 Linq:
List<string> d = idMainDescriptionIcon[0][0]
.Where(x => x.StartsWith("202"))
.Select(x => x)
.ToList();
我在idMainDescriptionIcon[0][0]宣告中遇到錯誤:
錯誤 CS1061“char”不包含“StartsWith”的定義,并且找不到接受“char”型別的第一個引數的可訪問擴展方法“StartsWith”(您是否缺少 using 指令或程式集參考?)
D 的值應為"202", "Thunderstorm", "thunderstorm with heavy rain", "11"。
這就是我被困的地方。不知道如何解決這個錯誤?
更新 #1
當洗掉 [0][0] 并用一個 [0] 替換它時,這是我得到的回報:

uj5u.com熱心網友回復:
這里的問題是idMainDescriptionIcon[0][0],這里指的是單個字串。迭代它會迭代字串中的字符,這就是為什么你會得到錯誤'char' does not contain a definition for 'StartsWith'
您需要的是以下內容
var d = idMainDescriptionIcon
.Where(x => x[0].StartsWith("202"))
.SelectMany(x => x)
.ToList();
您需要查詢整個idMainDescriptionIcon內部陣列的第一個元素以“202”開頭。
或者,
var d = idMainDescriptionIcon
.FirstOrDefault(x => x[0].StartsWith("202"))
.ToList();
輸出

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/336518.html
