C# 错误CS0161';RateController.GetRateById(长)和#x27;:并非所有代码路径都返回值
我有个错误 严重性代码描述项目文件行抑制状态错误CS0161“RateController.GetRateById(long)”:并非所有代码路径都返回值shenoywebapi D:\shenoystudio\shenoywebapi\Controllers\RateController.cs 192 ActiveC# 错误CS0161';RateController.GetRateById(长)和#x27;:并非所有代码路径都返回值,c#,asp.net-mvc,C#,Asp.net Mvc,我有个错误 严重性代码描述项目文件行抑制状态错误CS0161“RateController.GetRateById(long)”:并非所有代码路径都返回值shenoywebapi D:\shenoystudio\shenoywebapi\Controllers\RateController.cs 192 Active [Route("api/rate/getrate/{id}")] public Rate GetRateById(long id) {
[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;
}
}
只有在这一部分,您才有返回语句:
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;
GetRateById()需要返回一个速率。移动返回结果代码>向下一行,超出if
语句可能重复的
if (dsRateDetails.Tables[0].Rows.Count > 0)
return result;