Ricardo
Ricardo

Reputation: 19

Cannot implicitly convert type Collections.Generic.List to System.Collections.Generic.IEnumerable

I know this question has been asked many times, and I've tried everything to solve this problem. When I am trying to return, it comes out with convert type.

My classes:

public class BurgerJoint
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string ContactNumber { get; set; }  
    public string Description { get; set; }
    public bool Verified { get; set; }
    public DateTime DateCreated { get; set; }

    public ICollection<BurgerReview> BurgerReviews { get; set; }
    public ICollection<Location> Locations { get; set; }
}

public class Location
{
    public int Id { get; set; }
    public string Name { get; set; }
    public double Latitude { get; set; }
    public double Longitude { get; set; }
}

This is my implementation in my repository:

public IEnumerable<BurgerJoint> GetNearesBurgerJoints()
{
    var result = (from b in _context.BurgerJoints
                  join l in _context.Locations on b.Id equals l.Id
                  select new
                  {
                      b.Name,
                      b.Description,
                      l.Latitude,
                      l.Longitude

                  }).ToList();

    return result;

Upvotes: 0

Views: 3466

Answers (1)

Mostafiz
Mostafiz

Reputation: 7352

Create a new viewmodel class like

public class LocationViewModel
{
    public string Name { get; set; }
    public string Description { get; set; }
    public double Latitude { get; set; }
    public double Longitude { get; set; }

}

and then make your method like

public List<LocationViewModel> GetNearesBurgerJoints()
{

        var result = (from b in _context.BurgerJoints
                      join l in _context.Locations on b.Id equals l.Id
                      select new LocationViewModel
                      {
                          Name = b.Name,
                          Description = b.Description,
                          Latitude = l.Latitude,
                          Longitude = l.Longitude

                      }).ToList();


        return result; 
}

Upvotes: 1

Related Questions