0
public int Queue()
    {
        using (Entities server = new Entities())
        {
            var ServerId1 = (from serverID in server.AppPM_Patches
                             where serverID.PatchStatus == "NotStarted" && serverID.ServerId == 1
                             select serverID.ServerId).Count();
            var ServerId2 = (from serverID in server.AppPM_Patches
                             where serverID.PatchStatus == "NotStarted" && serverID.ServerId == 2
                             select serverID.ServerId).Count();
            var ServerId3 = (from serverID in server.AppPM_Patches
                             where serverID.PatchStatus == "NotStarted" && serverID.ServerId == 3
                             select serverID.ServerId).Count();
            if (ServerId1 == 0 && ServerId2 == 0 && ServerId3 == 0)
            {
                return ServerId1;//Convert.ToInt32(ServerId1);

            }
            else if (ServerId1 == 1 && ServerId2 == 0 && ServerId3 == 0)
            {
                return ServerId2;
            }
            else if (ServerId1 == 1 && ServerId2 == 1 && ServerId3 == 0)
            {
                return ServerId3;
            }
            else if (ServerId1 > ServerId2 && ServerId1 > ServerId3)
            {
                if (ServerId2 > ServerId3)
                {
                    return ServerId3;
                }
                else
                {
                    return ServerId2;
                }
            }
            else if (ServerId2 > ServerId3 && ServerId2 > ServerId1)
            {
                if (ServerId1 > ServerId3)
                {
                    return ServerId3;
                }
                else
                {
                    return ServerId1;
                }
            }
            else if (ServerId3 > ServerId1 && ServerId3 > ServerId2)
            {
                if (ServerId1 > ServerId2)
                {
                    return ServerId2;
                }
                else
                {
                    return ServerId1;
                }
            }
            else if (ServerId1 == ServerId2 && ServerId2 == ServerId3 && ServerId1 == ServerId3)
            {
                return ServerId1;
            }
            else if (ServerId1 == ServerId2 && ServerId1 > ServerId3 && ServerId2 > ServerId3)
            {
                return ServerId3;
            }
            else if (ServerId2 == ServerId3 && ServerId2 > ServerId1 && ServerId3 > ServerId1)
            {
                return ServerId1;
            }
            else if (ServerId1 == ServerId3 && ServerId1 > ServerId2 && ServerId1 > ServerId3)
            {
                return ServerId1;
            }
        }
    }

我在方法中收到错误(并非所有代码路径都返回值)。此方法返回我定义的 int 值。但是即使我返回了一个 int 值,也不知道为什么会发生这个错误。任何人都可以帮助我吗?

4

6 回答 6

5

你有if一堆else if陈述,但你没有else陈述。就编译器而言,完全可以想象if不满足任何条件。在这种情况下,该方法没有返回值。

您可以在else语句中或if通常在构造之后返回值。或者,如果代码确实应该在不满足任何条件的情况下退出if语句,那么您可以在方法的末尾抛出异常以指示存在错误。异常是方法可接受的退出策略。

于 2012-09-19T18:27:28.887 回答
3

这里还有很多其他答案可以解决您的特定错误消息,但是您的方法还有更多潜在问题被忽略了。您有大量的 if/else if 语句,它们非常长且复杂,只是为了返回 3 个整数中的最小值。有简单的方法可以做到这一点。重构最少的方法是在查询后立即添加以下内容:

return new[] { ServerId1, ServerId2, ServerId3  }.Min();

但是,您可以重构查询本身,以便只执行一个查询,而不是三个,并且不要在三个不同的服务器 ID 中硬编码。这将允许您将其中的所有内容替换为using

return (from serverID in server.AppPM_Patches
where serverID.PatchStatus == "NotStarted"
group serverID  by serverID.ServerId into servers
select servers.Count())
.Min();
于 2012-09-19T18:57:30.547 回答
1
    public int Queue()
        {

                using (Entities server = new Entities())
                {
int retVal=0;//initialize it your value
                    var ServerId1 = (from serverID in server.AppPM_Patches
                                     where serverID.PatchStatus == "NotStarted" && serverID.ServerId == 1
                                     select serverID.ServerId).Count();
                    var ServerId2 = (from serverID in server.AppPM_Patches
                                     where serverID.PatchStatus == "NotStarted" && serverID.ServerId == 2
                                     select serverID.ServerId).Count();
                    var ServerId3 = (from serverID in server.AppPM_Patches
                                     where serverID.PatchStatus == "NotStarted" && serverID.ServerId == 3
                                     select serverID.ServerId).Count();
                    if (ServerId1 == 0 && ServerId2 == 0 && ServerId3 == 0)
                    {
                        retVal=ServerId1;//Convert.ToInt32(ServerId1);

                    }
                    else if (ServerId1 == 1 && ServerId2 == 0 && ServerId3 == 0)
                    {
                        retVal=ServerId2;
                    }
                    else if (ServerId1 == 1 && ServerId2 == 1 && ServerId3 == 0)
                    {
                        retVal=ServerId3;
                    }
                    else if (ServerId1 > ServerId2 && ServerId1 > ServerId3)
                    {
                        if (ServerId2 > ServerId3)
                        {
                            retVal= ServerId3;
                        }
                        else
                        {
                            retVal=ServerId2;
                        }
                    }
                    else if (ServerId2 > ServerId3 && ServerId2 > ServerId1)
                    {
                        if (ServerId1 > ServerId3)
                        {
                            retVal=ServerId3;
                        }
                        else
                        {
                            retVal=ServerId1;
                        }
                    }
                    else if (ServerId3 > ServerId1 && ServerId3 > ServerId2)
                    {
                        if (ServerId1 > ServerId2)
                        {
                            retVal=ServerId2;
                        }
                        else
                        {
                            retVal=ServerId1;
                        }
                    }
                    else if (ServerId1 == ServerId2 && ServerId2 == ServerId3 && ServerId1 == ServerId3)
                    {
                        retVal=ServerId1;
                    }
                    else if (ServerId1 == ServerId2 && ServerId1 > ServerId3 && ServerId2 > ServerId3)
                    {
                        retVal=ServerId3;
                    }
                    else if (ServerId2 == ServerId3 && ServerId2 > ServerId1 && ServerId3 > ServerId1)
                    {
                        retVal=ServerId1;
                    }
                    else if (ServerId1 == ServerId3 && ServerId1 > ServerId2 && ServerId1 > ServerId3)
                    {
                        retVal=ServerId1;
                    }

        return retVal;
                }
        }
于 2012-09-19T18:41:38.467 回答
1

您没有其他案例,因此您没有涵盖所有案例。您可能在逻辑上涵盖了所有情况,但在语法上您没有。所以也许你可能想返回第一个服务器 ID 并添加

else
{
    return ServerId1;
}
于 2012-09-19T18:26:49.813 回答
0

else if如果没有满足s or中的至少一个if,则没有“默认”返回。

在最后添加 a return,或将最后else if一个更改为else.

于 2012-09-19T18:26:55.670 回答
0

如果你到达函数的末尾,你还没有返回任何东西。

于 2012-09-19T18:27:16.347 回答