【发布时间】:2019-02-18 15:46:37
【问题描述】:
此 LINQ 语句中的 ToDictionary() 方法调用需要参数。就目前而言,ToDictionary 部分是红色波浪线,因为缺乏更好的技术术语。错误:没有重载需要 0 个参数。是的,我知道。
我无法将 lambda 添加到 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 活动
【问题讨论】:
-
.ToDictionary(item => item.Prop, item => item.Att);: 你应该提供 key 和 value -
另外你不需要
AsEnumerable。 -
再一次,我无法在 ToDictionary 调用中添加 lambda。
-
请检查您是否有
using System.Linq;,因为ToDictionary()是一个扩展方法。 -
@KayBee718 当 itellisense 开始为您的 lambda 建议内容时,只需按 ESC。我也觉得很烦。