簡體   English   中英

錯誤 CS0161 'RateController.GetRateById(long)':並非所有代碼路徑都返回值

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

我有一個錯誤

嚴重性代碼描述項目文件行抑制狀態錯誤 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;
           }
       }

這是我的費率模型

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

        public DateTime wefDate { get; set; }

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

    }
}

這是我的 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; }
    }
}

只有在這部分你有一個 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 語句,則沒有 return 語句

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

您應該在該方法的最后一個花括號之前添加它:

return result;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM