ASP.NET CORE WEB API-无法隐式转换类型' response< system.collections.generic.generic.list< merchant.Response.MerchantMonthlysumdto>>>>

发布于 2025-02-02 15:40:31 字数 2893 浏览 2 评论 0原文

在我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏有森光若流苏 2025-02-09 15:40:31

您正在从async方法中返回错误的类型。

更改

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

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

和一切都会编译

You are returning the wrong type from the async method.

Change

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

to

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

and everything will compile

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文