Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ASP.NET Core Web API – Cannot implicitly convert type 'Models.Response<System.Collections.Generic.List<Merchant.Response.MerchantMonthlySumDto>>'

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading