[c#]Linq 左、 右括号不匹配

发布时间: 2017/2/25 14:03:05
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有此代码︰

using (Entities db = new Entities())
            {
              refer =  db.Refferals.Where(r =>
                r.RefferalDetails.Any(rd =>
                (Name.Contains(rd.Name) || rd.Name.Contains(Name) || LastName.Contains(rd.LastName) || rd.LastName.Contains(LastName) 
                || Company.Contains(r.Company) || r.Company.Contains(Company)
                Mobile.Contains(rd.Mobile) || rd.Mobile.Contains(Mobile))) &&
                Mobile.Length > 9 && Name.Length > 1 && LastName.Length > 1 && Company.Length > 2
                ).ToArray();

            }

虽然是括号算正确但编辑器提供一个额外的括号。enter image description here

我试着重新启动 Visual Studio,但也是一样。

解决方法 1:

尝试添加失踪 || 之间 r.Company.Contains(Company)Mobile.Contains(rd.Mobile)

修复缩进,这使得它更容易看出了什么错误。当我缩进它时,缺少运算符伸出像酸痛的拇指,和容易看到如何工作的父母。

using (Entities db = new Entities())
{
    refer = db.Refferals.Where(r =>
        r.RefferalDetails.Any(rd => 
            (
                Name.Contains(rd.Name) 
                || rd.Name.Contains(Name) 
                || LastName.Contains(rd.LastName) 
                || rd.LastName.Contains(LastName)
                || Company.Contains(r.Company) 
                || r.Company.Contains(Company)

                || /* <-- ADDED OR OPERATOR HERE */
                   Mobile.Contains(rd.Mobile) 

                || rd.Mobile.Contains(Mobile)
            )
        ) 
        && Mobile.Length > 9 
        && Name.Length > 1 
        && LastName.Length > 1 
        && Company.Length > 2
    ).ToArray();
}
赞助商