c# - 代码中的无效表达式项错误

标签 c# asp.net asp.net-mvc-4

  • 错误 1 ​​'int.TryParse(string, out int)' 的最佳重载方法匹配有一些无效参数
  • 错误 2 参数 1:无法从“int”转换为“string”

    它在“int.TryParse(surveys.First(), out id);”中给我错误

       L
          var surveys = (from su in DbContext.Surveys
                                   where su.userName == su.userName 
                                   select su.ID);
    if(surveys.Count() > 0)
                {
                 int id = 0;
                 int.TryParse(surveys.First(), out id);
                   return id;
                 }
                 return 0;
    

最佳答案

从 TryParse() 中删除 int;

int.TryParse(surveys.First(), out int id);

应该是

int.TryParse(surveys.First(), out id);

改变:-

List<SurveyContext> surveys = (from su in DbContext.Surveys
                                          where su.userName == su.userName 
                                          select su.ID).ToList();

List<string> surveys = (from su in DbContext.Surveys
                                          where su.userName == su.userName 
                                          select su.ID);

您正在尝试选择 string输入 Linq 并将其放入 List<SomeType>应该是 List<int> .

  var surveys = (from su in DbContext.Surveys
                                              where su.userName == su.userName 
                                              select su.ID);

  //Code follows
  int.TryParse(surveys.First(), out id);

关于c# - 代码中的无效表达式项错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16680507/

相关文章:

c# - List.All() 和 List.TrueForAll() 之间的区别

c# - 如何处理 OO 应用程序中的横切关注点?使用单例?依赖注入(inject)?什么?

asp.net - 下拉列表值和文本需要修剪

asp.net - 构建 ASP.NET 应用程序 - 最佳实践

javascript - 从页面 HTML 获取动态元素?

c# - 未使用“Autofac.Core.Activators.Reflection.DefaultConstructorFinder”找到任何构造函数

c# - 如何设置正确的用户名和密码文本框?

asp.net - 如何列出所有已安装的 NuGet 包?

asp.net-mvc-4 - 使用 NServiceBus 和 MVC 的 StructureMap 管理 RavenDB IDocumentSession 生命周期

javascript - 如何使 "Select"文本在下拉菜单中不可见(由 <select> <option> 标签创建)?