如果和其他如果得到错误方法返回datatable c#

时间:2018-04-13 17:56:50

标签: c#

当我编写一个使用if和else if语句返回数据表的方法时,我收到错误。什么是正确的格式。提前致谢 "并非所有代码路径都返回值"

    [HttpGet]
    public DataTable getcareerdata (int tclass, int efkey)
    {
        if (tclass ==7)
        {
            return obj.GetData(string.Format(@"select Effect_Class_Key , From_Value , To_Value , Effect_Class_Value, Scientific_Degree, case When Scientific_Degree  is null then 'Not Related'
            when Scientific_Degree  = 0 then 'Not Related' when Scientific_Degree  = -1 then 'Not Related' else latin_desc  end as Spec from hr_effect_classes Left Outer Join general_cod 
            on hr_effect_classes.Scientific_Degree = general_cod .sub_cod and  main_cod = 17001  where type_class in (7) and status_class = 1  and effect_key = {0}
            Order By  Scientific_Degree  , From_Value ", efkey));
        }
        else if (tclass == 8)
        {
            return obj.GetData(string.Format(@"select Effect_Class_Key ,  From_Value , To_Value , Effect_Class_Value, Scientific_Degree case When Scientific_Degree is null then 'Not Related'
            when Scientific_Degree  = 0 then 'Not Related' when Scientific_Degree  = -1 then 'Not Related' else L_Desc end as Spec from hr_effect_classes Left Outer Join HR_Specialty   
            on hr_effect_classes.Scientific_Degree = HR_Specialty.Spec_Key where type_class in (8) and status_class = 1 and effect_key = {0}
            Order By  Scientific_Degree  , From_Value ",efkey));
        }
    }

2 个答案:

答案 0 :(得分:2)

如果你们两个if条件都失败了那么你的方法没有什么可以返回的,而你的方法签名告诉编译器它会返回一个类型为DataTable的对象,如果在上述情况下它将不会这样做。

那么,应该做的是使用类型为DataTable的局部变量,并在条件块中相应地设置它的值,然后最终从方法返回变量。

将您的方法重构为:

DataTable table = null;
if (tclass ==7)
{
    table  = obj.GetData(string.Format(@"select Effect_Class_Key , From_Value , To_Value , Effect_Class_Value, Scientific_Degree, case When Scientific_Degree  is null then 'Not Related'
            when Scientific_Degree  = 0 then 'Not Related' when Scientific_Degree  = -1 then 'Not Related' else latin_desc  end as Spec from hr_effect_classes Left Outer Join general_cod 
            on hr_effect_classes.Scientific_Degree = general_cod .sub_cod and  main_cod = 17001  where type_class in (7) and status_class = 1  and effect_key = {0}
            Order By  Scientific_Degree  , From_Value ", efkey));
}
else if (tclass == 8)
{
    table  = obj.GetData(string.Format(@"select Effect_Class_Key ,  From_Value , To_Value , Effect_Class_Value, Scientific_Degree case When Scientific_Degree is null then 'Not Related'
            when Scientific_Degree  = 0 then 'Not Related' when Scientific_Degree  = -1 then 'Not Related' else L_Desc end as Spec from hr_effect_classes Left Outer Join HR_Specialty   
            on hr_effect_classes.Scientific_Degree = HR_Specialty.Spec_Key where type_class in (8) and status_class = 1 and effect_key = {0}
            Order By  Scientific_Degree  , From_Value ",efkey));
}

 return table;

现在我们返回一个DataTable类型的引用,虽然如果两个条件都失败但它会为null但它会使你的代码编译器在没有任何构建时错误的情况下变为有效。

答案 1 :(得分:2)

无论如何都需要总是返回一些东西,所以如果不满足条件,则将return null添加到if语句之外。

[HttpGet]
public DataTable getcareerdata (int tclass, int efkey)
{
    if (tclass ==7)
    {
        return obj.GetData(string.Format(@"select Effect_Class_Key , From_Value , To_Value , Effect_Class_Value, Scientific_Degree, case When Scientific_Degree  is null then 'Not Related'
        when Scientific_Degree  = 0 then 'Not Related' when Scientific_Degree  = -1 then 'Not Related' else latin_desc  end as Spec from hr_effect_classes Left Outer Join general_cod 
        on hr_effect_classes.Scientific_Degree = general_cod .sub_cod and  main_cod = 17001  where type_class in (7) and status_class = 1  and effect_key = {0}
        Order By  Scientific_Degree  , From_Value ", efkey));
    }
    else if (tclass == 8)
    {
        return obj.GetData(string.Format(@"select Effect_Class_Key ,  From_Value , To_Value , Effect_Class_Value, Scientific_Degree case When Scientific_Degree is null then 'Not Related'
        when Scientific_Degree  = 0 then 'Not Related' when Scientific_Degree  = -1 then 'Not Related' else L_Desc end as Spec from hr_effect_classes Left Outer Join HR_Specialty   
        on hr_effect_classes.Scientific_Degree = HR_Specialty.Spec_Key where type_class in (8) and status_class = 1 and effect_key = {0}
        Order By  Scientific_Degree  , From_Value ",efkey));
    }
return null;
}