ASP.NET Core Web API - 无法隐式转换类型 'Models.Response<System.Collections.Generic.List<Merchant.Response.MerchantMonthlySumDto>>'

ASP.NET Core Web API - Cannot implicitly convert type 'Models.Response<System.Collections.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;
}

我想获取授权的每月总和数据,但出现此错误:

Error CS0029 Cannot implicitly convert type 'Models.Response<System.Collections.Generic.List<Merchant.Response.MerchantMonthlySumDto>>' to 'System.Collections.Generic.List<Merchant.Response.MerchantMonthlySumDto>'

然后响应return响应中高亮显示;

我哪里遗漏了它,我该如何解决这个问题?

谢谢

您从 async 方法返回了错误的类型。

改变

public async Task<List<MerchantMonthlySumDto>> GetMandateMonthlySum()

public async Task<Response<List<MerchantMonthlySumDto>>()> GetMandateMonthlySum()

一切都会编译