简体   繁体   English

当变量实际上在正确的范围内时出现错误 CS0103

[英]Error CS0103 when a variable is actually in the right scope

I have this control (db is the Entity Framework context):我有这个控制(db 是实体框架上下文):

if (db.Sites.Any(s => s.Name.Equals(name))) throw new NameAlreadyInUseException(name);

When I run my tests and debug it fails giving me the error:当我运行我的测试和调试它失败给我错误:

Error CS0103: the name 's' does not exist in the current context.

I honestly can't get my head around it and Google hasn't really been helping... any help is appreciated, thanks in advance.老实说,我无法理解它,谷歌并没有真正提供帮助......感谢任何帮助,提前致谢。 Isn't s used correctly here? s 在这里使用不正确吗? (I'm still learning, so maybe I missed something but my code here looks ok to me) (我仍在学习,所以也许我错过了一些东西,但我的代码在这里看起来不错)

Edit: the debugger triggers the error on this line and I am not using s in any other place other than inside that if statement.编辑:调试器在这一行触发错误,我没有在 if 语句之外的任何其他地方使用 s 。 (I edited the line to show what happens with the if) (我编辑了该行以显示 if 会发生什么)

Edit2: complete code of the function Edit2:函数的完整代码

public void CreateSiteOnDb(string connectionString, string name, int timezone, int sessionExpirationTimeInSeconds,
            double minimumBidIncrement)
        {
            CheckInput_CreateSiteOnDb(connectionString, name, timezone, sessionExpirationTimeInSeconds, minimumBidIncrement);

            try
            {
                using (var db = new AuctionSiteContext(connectionString))
                {
                    if (db.Sites.Any(s => s.Name.Equals(name))) throw new NameAlreadyInUseException(name);

                    var site = new Entities.Site
                    {
                        Name = name,
                        Timezone = timezone,
                        MinimumIncrement = minimumBidIncrement,
                        SessionExpirationInSeconds = sessionExpirationTimeInSeconds
                    };

                    db.Sites.Add(site);
                    db.SaveChanges();
                }
            }
            catch(NameAlreadyInUseException)
            {
                throw;
            }
            catch(Exception)
            {
                throw new UnavailableDbException();
            }
        }

Edit3: Error as shown during debugging Edit3:调试过程中显示的错误

来自 Visual Studio 的屏幕截图显示调试期间的错误

It is not in the right scope.它不在正确的范围内。

In the screenshot you are in the scope of CreateSiteOnDb -> try -> using , but s does not belong to that context.在屏幕截图中,您在CreateSiteOnDb -> try -> using的范围内,但s不属于该上下文。

In very basic terms s => .... is converted to function of a class, and it is called from inside Any .用非常基本的术语s => ....转换为类的函数,并从Any内部调用它。 so let's assume that our expression is function named Steve .所以让我们假设我们的表达式是名为Steve函数。 Steve would look like this: Steve看起来像这样:

bool Steve(ISite s)
{
  return s.Name.Equals(name);
}

this means s is the parameter of Steve , and is only valid inside Steve , which becomes CreateSiteOnDb -> try -> using -> Any -> Steve这意味着sSteve的参数,并且只在Steve内部有效,它变成CreateSiteOnDb -> try -> using -> Any -> Steve

So to see s you need to be in two more levels.因此,要查看s您还需要再进入两个级别。 Please, put your cursor inside the expression and then put a breakpoint.请将光标放在表达式中,然后放置一个断点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM