Home > database >  I am looking to retrieve a list of regions using IEnumerable
I am looking to retrieve a list of regions using IEnumerable

Time:08-08

I am creating a new project to practice by my self, I am looking to return a list using GetAll method, but I'm struggling with it. I'll show the code here so hopefully someone can help.

I am using ASP.NET Core 6 and SQL Server for the data.

namespace Northwind.Web.Data.Repositories.Regions
{
    public interface IRegionRespository
    {
        Task<bool> Create(CreateRegionViewModel region);
        Task<bool> Update(RegionViewModel region);
        Task<RegionViewModel> GetById(int id);
        Task<IEnumerable<RegionViewModel>> GetAll();
    }
}

async Task<IEnumerable<RegionViewModel>> IRegionRespository.GetAll()
{
    var regionList = _nordwinContext.Region.ToList();
    return regionList;

    //var query = _nordwinContext.Region.AsNoTracking().AsQueryable();
}

    
    public class Region
    {
        public int RegionId { get; set; }
        public string RegionDescription { get; set; }
    }

CodePudding user response:

As mentioned in the comments you need to add your code as text and provide any errors you get in order for anyone to help your properly.

However, I'm going to take a stab in the dark and say that the images you uploaded indicates that you're neglecting to return the actual value of your method, or the list. You need to add the return keyword with the value you get from your data context.

async Task<IEnumerable<RegionViewModel>> GetAll()
{
   var regionList = _nordwindContext.Region.Tolist();
   return regionList;
}
 

Edit 1 (received further details of the error)

The method GetAll() wants to return a type RegionViewModel, but your data context is returning the type Region. In order to get around that you should change one of them. Let's try with the Interface and implementation.

 async Task<IEnumerable<RegionViewModel>> IRegionRespository.GetAll()
        {
            var list = _nordwinContext.Region
                 .Select(r => new RegionViewModel
                 {
                     RegionDescription = r.RegionDescription,
                     RegionId = r.RegionId
                 }).ToList();

            return list;
            //var regionList = _nordwinContext.Region.ToList();
            //return regionList;

        }
  • Related