Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issues with LINQ statement with defining an element of a collection

Tags:

c#

linq

I'm a student taking the ASP.NET core fundamentals on pluralsight by Scott Allen. I'm having issues with a LINQ statement where r is not defined. The whole file is below but I am really only having issues with the last part and defining r. r is not defined in this file or any of the other files I have worked with in the course thus far. In the video Scott just seems to type r no problem and it recognizes r as a restaurant. When i do the same r is not recognized as anything, I feel like I am missing something pretty basic here as to why r is not being recognized or I need to define it somewhere else.

public interface IRestaurantData
{
    IEnumerable<Restaurant> GetAll();
}
public class InMemoryRestaurantData : IRestaurantData
{
    readonly List<Restaurant> restaurants;
    public InMemoryRestaurantData()
    {
        restaurants = new List<Restaurant>()
        {
            new Restaurant { Id = 1, Name = "Scott's Pizza", Location = "Maryland", Cuisine = CuisineType.Italian},
            new Restaurant { Id = 2, Name = "Cinnamon Club", Location = "London", Cuisine = CuisineType.Indian},
            new Restaurant { Id = 3, Name = "La Costa", Location = "California", Cuisine = CuisineType.Mexican}
        };
    }
    public IEnumerable<Restaurant> GetAll()
    {
        return from r in restaurants
               orderby r.Name
               select r;
    }
}
namespace OdeToFood.Core
{
    public class Restaurant
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Location { get; set; }
        public CuisineType Cuisine { get; set; }
    }
}
like image 482
S. Curtis Avatar asked May 14 '19 12:05

S. Curtis


2 Answers

Seems like the using statement using System.Linq is missing. Try adding it and it should work.

like image 80
Suhas Kulkarni Avatar answered Oct 25 '22 13:10

Suhas Kulkarni


Add using System.Linq to your namespaces. If you are using VS, just press CTRL+.

like image 30
bolkay Avatar answered Oct 25 '22 13:10

bolkay