此 LINQ 語句中的 ToDictionary() 方法調用需要參數。就目前而言,ToDictionary 部分是紅色波浪線,因為缺乏更好的技術術語。錯誤:沒有重載需要 0 個參數。是的,我知道。我無法將 lambdas 添加到 ToDictionary 方法調用,因為 Intellisense 正在用它的建議覆蓋我的 lambda。換句話說,如果我輸入“x”,它會用 XmlReader 替換它。啊。我已經嘗試過使用和不使用 AsEnumerable。我從 StackOverflow 帖子中借用了大部分代碼,但我添加了字典部分。我在某處缺少括號還是什么?哈哈!var props = (from p in _type.GetProperties() let attr = p.GetCustomAttribute<ExcelExportAttribute>() where attr != null && attr.ReportId.ToString() == reportID select new {Prop = p, Att = attr }) .AsEnumerable() .ToDictionary<PropertyInfo, ExcelExportAttribute>();VS 中的錯誤嚴重性代碼描述項目文件行抑制狀態錯誤 CS1929“IEnumerable<>”不包含“ToDictionary”的定義,最佳擴展方法重載“Enumerable.ToDictionary(IEnumerable,Func,IEqualityComparer)”需要“IEnumerable”類型的接收器WFG.UtilityLib.Excel C:\Users\kbessel\source\repos\WFG.UtilityLib.Excel\WFG.UtilityLib.Excel\ExcelExport.cs 142 活動
2 回答

莫回無
TA貢獻1865條經驗 獲得超7個贊
您需要完全省略泛型類型,如下所示:
.ToDictionary(x => x.Prop, x => x.Att);
原因是擴展方法不需要兩個而是三個泛型類型:一個用于“this”參數,另外兩個用于“常規”參數 - 或者沒有,因為編譯器可以從參數派生類型。
您可以顯式指定所有 3 種類型,但這幾乎沒有任何用途,因為它們可以自動派生。

慕田峪7331174
TA貢獻1828條經驗 獲得超13個贊
我無法在ToDictionary方法調用中添加 lambda,因為 Intellisense 用它的建議覆蓋了我的 lambda。換句話說,如果我輸入“x”,它將替換為XmlReader.
這是一個需要克服的簡單問題:輸入x,然后按下Esc關閉 Intellisense 下拉菜單。根據需要繼續鍵入表達式:
var props = _type.GetProperties()
.SelectMany(p => new {Prop = p, Attr = p.GetCustomAttribute<ExcelExportAttribute>()})
.Where(p => p?.ReportId?.ToString() == reportId)
.ToDictionary(p => p.Prop, p => p.Attr);
- 2 回答
- 0 關注
- 237 瀏覽
添加回答
舉報
0/150
提交
取消