Invalid expression term error in the code

  • Error 1 The best overloaded method match for 'int.TryParse(string, out int)' has some invalid arguments
  • Error 2 Argument 1: cannot convert from 'int' to 'string'

    It gives me error in "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;
    

  • Remove int from TryParse();

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

    should be

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

    Change :-

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

    to

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

    You are trying to select string type in Linq and putting it in List<SomeType> which should be 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);
    

    It sounds like you want something like this. Move the int datatype to the declaration of the variable, and add a property field name after the First() method. (or whatever equivalent property you have on the Survey object that holds its Id)

    if(surveys.Count() > 0)
    {
        int id = 0;
        int.TryParse(surveys.First().Id, out id);
        return id;
    }
    return null;
    

    UPDATE : I see you've updated the question, it looks like what you have should do the trick..

    链接地址: http://www.djcxy.com/p/15892.html

    上一篇: SQL Server Express和C#:数据库图表

    下一篇: 代码中的表达式术语错误无效