简体   繁体   English

错误 CS0161 'RateController.GetRateById(long)':并非所有代码路径都返回值

[英]Error CS0161 'RateController.GetRateById(long)': not all code paths return a value

I have an error我有一个错误

Severity Code Description Project File Line Suppression State Error CS0161 'RateController.GetRateById(long)': not all code paths return a value shenoywebapi D:\\shenoystudio\\shenoywebapi\\Controllers\\RateController.cs 192 Active严重性代码描述项目文件行抑制状态错误 CS0161 'RateController.GetRateById(long)':并非所有代码路径都返回值 shenoywebapi D:\\shenoystudio\\shenoywebapi\\Controllers\\RateController.cs 192 Active

       [Route("api/rate/getrate/{id}")]
       public Rate GetRateById(long id)
       {
           var result = new Rate();

           var dsRate = SqlHelper.ExecuteDataset(AppDatabaseConnection, CommandType.StoredProcedure, 0, "GetRateById", new SqlParameter("@Id", id));

           if (dsRate.Tables[0].Rows.Count > 0)
           {
               DataRow row = dsRate.Tables[0].Rows[0];
               result = new Rate
               {
                   Id = Convert.ToInt64(row[0]),
                   wefDate = Convert.ToDateTime(row[1]),
                   //ProductRates =new List<ProductRate>() { new ProductRate() { Rate = Convert.ToInt64(row[2])} }
               };

           }
           var ProductRatesList = new List<ProductRate>();
           var dsRateDetails = SqlHelper.ExecuteDataset(AppDatabaseConnection, CommandType.StoredProcedure, 0, "GetRateDetailsById", new SqlParameter("@RateId", id));

           if (dsRateDetails.Tables[0].Rows.Count > 0)
           {
               foreach (DataRow row in dsRateDetails.Tables[0].Rows)
               {
                   ProductRatesList.Add(new ProductRate
                   {
                       Rate = Convert.ToDecimal(row[0])
                   });
               }
               result.ProductRates = ProductRatesList;
               return result;
           }
       }

This is my rate model这是我的费率模型

{
    public class Rate
    {
        public long Id { get; set; }

        public DateTime wefDate { get; set; }

        public IList<ProductRate> ProductRates { get; set; }

    }
}

This is my ProductRate Model这是我的 ProductRate 模型

namespace shenoywebapi.Models
{
    public class ProductRate
    {
        public long Id { get; set; }

        public string SerialNumber { get; set; }

        public long ProductId { get; set; }

        public string ProductName { get; set; }

        public string Unit { get; set; }

        public decimal Rate { get; set; }
    }
}

Only on this part you have a return statement:只有在这部分你有一个 return 语句:

if (dsRateDetails.Tables[0].Rows.Count > 0)
       {
           foreach (DataRow row in dsRateDetails.Tables[0].Rows)
           {
               ProductRatesList.Add(new ProductRate
               {
                   Rate = Convert.ToDecimal(row[0])
               });
           }
           result.ProductRates = ProductRatesList;
           return result;
       }

if the code goes not to this if statement, there is no return statement如果代码没有转到这个 if 语句,则没有 return 语句

if (dsRateDetails.Tables[0].Rows.Count > 0)

you should add this before the last curly brace of the method:您应该在该方法的最后一个花括号之前添加它:

return result;

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

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