Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I am facing a scenario where I am trying to return a list of "ProductTypes". I may need this list in many places in the code and so defined an async method and then calling that method from another async [HttpGet] method and trying to retrun the resut to the frontend.

I can understand we cannot convert an Interface to another Interface, but since am returning a list of ProductTypes, so it shouldn't have been any problem.

The async method is shown below:

<pre><pre>private async Task<IEnumerable<ProductTypesData>> GetProductTypes() {
            return await _context.ProductTypePool
                .Where(ProdType => ProdType.ProdParentTypeId == 0 && ProdType.IsActive == true)
                .OrderBy(ProdType => ProdType.ProdTypeId)
                .Select(ProdType => new ProductTypesData { ProdTypeId = ProdType.ProdTypeId, ProdTypeName = ProdType.ProdTypeName, ProdTypeDesc = ProdType.ProdTypeDesc})
                .ToListAsync();
        }


Then, am calling this method from annother async [HttpGet] method, as shown below:

[HttpGet]
        public async Task<ActionResult<IEnumerable<ProductTypesData>>> GetAllProductTypes()
        {
            return this.GetProductTypes();
        }


What I have tried:

I tried to wrapthe result into a concrete List class:

[HttpGet]
        public async Task<ActionResult<IEnumerable<ProductTypesData>>> GetAllProductTypes()
        {
            return new List<ProductTypesData>(this.GetProductTypes());
        }


But, this didn't work out.

I also tried to Use IList instead of IEnumerable but things are same there also.

Please help me knowing the way out in such conditions where I may have to return a list of data and that list may be used in many places in the code.

Thanks.
Posted
Updated 6-Feb-23 22:19pm
v2
Comments
Graeme_Grant 7-Feb-23 4:09am    
you're returning IEnumerable<ProductTypesData> not ActionResult<IEnumerable<ProductTypesData>>
Agnib Pyne 2022 7-Feb-23 4:32am    
Yes. Can I mark the async method (GetProductTypes()) as Task<actionresult<ienumerable>> too? If not, then can I mark [HttpGet] method as Task<ienumerable<...>> only since am not using any HttpResponseMessage within the [HttpGet] method. I am just returning the data to the UI and dispaying it.

You need to await the task returned from GetProductTypes.

Assuming you're using .NET Core / .NET 5+, you can then simply return that result - ActionResult<T> has an implicit conversion[^] from T:
C#
[HttpGet]
public async Task<ActionResult<IEnumerable<ProductTypesData>>> GetAllProductTypes()
{
    return await this.GetProductTypes();
}


Edit: Implicit conversions don't work for interfaces. You can either change the return type of the GetProductTypes method:
C#
private async Task<List<ProductTypesData>> GetProductTypes()
{
   ...
}

[HttpGet]
public async Task<ActionResult<IEnumerable<ProductTypesData>>> GetAllProductTypes()
{
    return await this.GetProductTypes();
}

Or explicitly wrap the results in an ActionResult<T>:
C#
[HttpGet]
public async Task<ActionResult<IEnumerable<ProductTypesData>>> GetAllProductTypes()
{
    return Ok(await this.GetProductTypes());
}
 
Share this answer
 
v2
Comments
Agnib Pyne 2022 7-Feb-23 4:30am    
While trying the above solution, am getting another error:

Awaited task returns 'interface System.Collections.Generic.IEnumerable<out t="">'
Exposes the enumerator, which supports a simple iteration over a collection of a specified type.

T is ProductTypesData
Richard Deeming 7-Feb-23 4:34am    
That's not the complete error message.
Agnib Pyne 2022 7-Feb-23 4:56am    
Awaited task returns 'interface System.Collections.Generic.IEnumerable<out t="">'
Exposes the enumerator, which supports a simple iteration over a collection of a specified type.

T is ProductTypesData
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<api.classes.producttypesdata>' to 'Microsoft.AspNetCore.Mvc.ActionResult<system.collections.generic.ienumerable<api.classes.producttypesdata>>' [API]csharp(CS0029)

There are 2 error messages, one is the same as earlier one so I didn't post that.
But now I have mentioned the full error messages
Richard Deeming 7-Feb-23 5:03am    
Which version of .NET are you using? According to the documentation, there is an implicit cast from T to ActionResult<T>, so there shouldn't be any problem implicitly converting IEnumerable<ProductTypesData> to ActionResult<IEnumerable<ProductTypesData>>. That should just work in everything from .NET Core 2.1 onwards.
Agnib Pyne 2022 7-Feb-23 5:25am    
I am using .NET version 6.0.401 for this project.
public async Task<actionresult<ienumerable<state>>> GetStates()
{
return await _context.States.ToListAsync();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900