c# - 为什么 Visual Studio 会在 .Net Core 上运行的 WebAPI 代码中报告 lambda 错误?

标签 c# visual-studio lambda asp.net-core resharper

我将一个 WebAPI 从 FullDotnet (4.6) 迁移到 .Net Core 2.0,我在使用 Dapper 的数据层中遇到了这个问题。

我的代码:

public List<Doctor> GetList()
{
    List<Doctor> ret;
    using (var db = GetMySqlConnection())
    {
        const string sql = @"SELECT D.Id, D.Bio, D.CRMState, D.CRMNumber,
        U.Id, U.Email, U.CreatedOn, U.LastLogon, U.Login, U.Name, U.Phone, U.Surname, U.Id, U.Birth, U.CPF,
        S.Id, S.Name
        from Doctor D
        inner join User U on U.Id = D.UserId
        inner join Speciality S on S.Id = D.IDEspeciality
        order by D.Id DESC";

        ret = db.Query<Doctor, User, Speciality, Doctor>(sql, (doctor, user, speciality) =>
        {
            doctor.User = user;
            doctor.Speciality = speciality;
            return doctor;
        }
        , splitOn: "Id, Id", commandType: CommandType.Text).ToList();
    }
    return ret;
}

奇怪的行为:

PrintScreen from code

解决方案构建并运行 VisualStudio 突出显示的“错误”是:

Argument type 'lambda expression' is not assignable to parameter type 'System.Func`5'

我有另一个具有相同行为和相同错误的类,但是代码编译并运行,但是这个“错误突出显示”很烦人!

我在运行 .NET Framework 4.6 的旧解决方案中没有这个亮点

有用的信息:

  • 类库:.Net Standard 2.0
  • Visual Studio 2017 v15.4.1
  • Resharper 2017.2

最佳答案

正如我们在评论中发现的那样,问题是由 ReSharper (2017.2) 引起的。

如果怀疑 ReSharper 有问题,您可以采取一些额外的步骤。

暂停/恢复 ReSharper

只是为了确认 ReSharper 导致了问题,您可以使用以下方法暂停它:

工具/选项.../Resharper 节点/立即暂停

在这种情况下,执行暂停/恢复循环就足够了,并且解决了问题。

清除 ReSharper 缓存 ( source )

Broken caches affect ReSharper behavior. For example, ReSharper can stop resolving symbols or some navigation commands can be unavailable. If you notice such strange behavior, clearing caches for the current solution could help in most cases.

To clear caches for the current solution

  1. Open the solution with supposedly broken caches in Visual Studio.
  2. Open the Environment | General page of ReSharper options.
  3. Click Clear caches. Note that the caches will be only cleaned in the currently selected cache location.
  4. Reopen your solution for the changes to take effect.

ReSharper also cleans up solution caches automatically if a specific solution was not open for more than 30 days.

如果上述方法不起作用怎么办?

如果您确认这是 ReSharper 问题,那么您应该首先尝试更新到 ReSharper 的最新版本并尝试重现您的问题。

如果问题仍然存在,那么您应该前往 JetBrains bug tracker并在确认它不是已报告的错误后提交问题。

关于c# - 为什么 Visual Studio 会在 .Net Core 上运行的 WebAPI 代码中报告 lambda 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46954286/

相关文章:

c# - Lambda 表达式,其中列等于列表项

c# - Linq:获取丢失的记录

c# - 如果 user.config 中有 Int32[] 属性,我在使用 CustomSettingsProvider 时会收到无效的转换异常

c# - 使用动态方法创建类

c# - 获取设备句柄时出错

c++ - visual c++ 强制发布构建使用调试构建代码

c++ - VS2005 中损坏的 std::map 可视化工具

.net - ToString() 和调试器的字符串可视化工具

c++ - 为什么将函数包装到 lambda 中可能会使程序更快?

c# - 有没有办法分解 Expression<Func<T, Bool>> 并获得相等比较的右侧?