Home > OS >  ASP.NET Core Web API - Cannot implicitly convert type 'Models.Response<System.Collections.Ge
ASP.NET Core Web API - Cannot implicitly convert type 'Models.Response<System.Collections.Ge

Time:05-29

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($"{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;
}

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

CodePudding user response:

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

  • Related