ASP.NET CORE WEB API-无法隐式转换类型' response< system.collections.generic.generic.list< merchant.Response.MerchantMonthlysumdto>>>>
在我的ASP.NET Core-6 Web API中,我正在创建一个统计仪表板,总结并在仪表板上显示了总和数据。
我有此DTO:
public class MerchantMonthlySumDto
{
public decimal ItemSum { get; set; }
public int Month { get; set; }
public string MonthName { get; set; }
}
那是响应模型:
public class Response<T>
{
public T Data { get; set; }
public bool Successful { get; set; }
public string Message { get; set; }
public int StatusCode { get; set; }
public Response(int statusCode, bool success, string msg, T data)
{
Data = data;
Successful = success;
StatusCode = statusCode;
Message = msg;
}
public Response()
{
}
public static Response<T> Fail(string errorMessage, int statusCode = 404)
{
return new Response<T> { Successful = false, Message = errorMessage, StatusCode = statusCode };
}
public static Response<T> Success(string successMessage, T data, int statusCode = 200)
{
return new Response<T> { Successful = true, Message = successMessage, Data = data, StatusCode = statusCode };
}
public override string ToString() => JsonConvert.SerializeObject(this);
}
我有服务接口:
public interface IMerchantDashboardService
{
Task<Response<List<MerchantMonthlySumDto>>> GetMandateMonthlySum();
}
最后一个服务实现:
public async Task<List<MerchantMonthlySumDto>> GetMandateMonthlySum()
{
var response = new Response<List<MerchantMonthlySumDto>>();
DateTime current = DateTime.Now;
DateTime currentYear = DateTime.Parse($"{current.Year}/01/01");
var monthlyMandate = _dbContext.Mandates.Where(m => m.CreatedAt >= currentYear).
.GroupBy(o => new
{
Month = o.CreatedAt.Month
})
.Select(u => new MerchantMonthlySumDto
{
ItemSum = u.Sum(x => x.Amount),
Month = u.Key.Month,
MonthName = CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName(u.Key.Month)
})
.ToList();
if (monthlyMandate != null)
{
response.StatusCode = (int)HttpStatusCode.OK;
response.Successful = true;
response.Data = monthlyMandate;
response.Message = $"are the statistics for the Admin";
return response;
}
response.Data = default;
response.StatusCode = (int)HttpStatusCode.BadRequest;
response.Successful = false;
response.Message = $"No record exists in the database";
return response;
}
我想获得授权的月度总和数据,但是我得到了此错误:
错误CS0029无法隐式转换类型'型号。到'system.collections.generic.list&lt; merchant.Response.meranthlysumdto&gt;'
然后响应在返回响应中突出显示;
我错过了它,如何解决这个问题?
谢谢
In my ASP.NET Core-6 Web API, I am creating a statistical dashboards that summarizes and presents the sum data on the dashboard.
I have this DTO:
public class MerchantMonthlySumDto
{
public decimal ItemSum { get; set; }
public int Month { get; set; }
public string MonthName { get; set; }
}
Then this is the Response model:
public class Response<T>
{
public T Data { get; set; }
public bool Successful { get; set; }
public string Message { get; set; }
public int StatusCode { get; set; }
public Response(int statusCode, bool success, string msg, T data)
{
Data = data;
Successful = success;
StatusCode = statusCode;
Message = msg;
}
public Response()
{
}
public static Response<T> Fail(string errorMessage, int statusCode = 404)
{
return new Response<T> { Successful = false, Message = errorMessage, StatusCode = statusCode };
}
public static Response<T> Success(string successMessage, T data, int statusCode = 200)
{
return new Response<T> { Successful = true, Message = successMessage, Data = data, StatusCode = statusCode };
}
public override string ToString() => JsonConvert.SerializeObject(this);
}
I have the service interface:
public interface IMerchantDashboardService
{
Task<Response<List<MerchantMonthlySumDto>>> GetMandateMonthlySum();
}
Finally the service implementation:
public async Task<List<MerchantMonthlySumDto>> GetMandateMonthlySum()
{
var response = new Response<List<MerchantMonthlySumDto>>();
DateTime current = DateTime.Now;
DateTime currentYear = DateTime.Parse(quot;{current.Year}/01/01");
var monthlyMandate = _dbContext.Mandates.Where(m => m.CreatedAt >= currentYear).
.GroupBy(o => new
{
Month = o.CreatedAt.Month
})
.Select(u => new MerchantMonthlySumDto
{
ItemSum = u.Sum(x => x.Amount),
Month = u.Key.Month,
MonthName = CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName(u.Key.Month)
})
.ToList();
if (monthlyMandate != null)
{
response.StatusCode = (int)HttpStatusCode.OK;
response.Successful = true;
response.Data = monthlyMandate;
response.Message = quot;are the statistics for the Admin";
return response;
}
response.Data = default;
response.StatusCode = (int)HttpStatusCode.BadRequest;
response.Successful = false;
response.Message = quot;No record exists in the database";
return response;
}
I suppose to get the monthly sum data for mandate, but I got this error:
Error CS0029 Cannot implicitly convert type 'Models.Response<System.Collections.Generic.List<Merchant.Response.MerchantMonthlySumDto>>' to 'System.Collections.Generic.List<Merchant.Response.MerchantMonthlySumDto>'
Then response is highlighted in return response;
Where have I missed it and how do I get this resolved?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在从
async
方法中返回错误的类型。更改
为
和一切都会编译
You are returning the wrong type from the
async
method.Change
to
and everything will compile