5

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; }
    }
}
5
  • 5
    Sorry but I cannot reproduce your problem (as long as you have an appropriate Restaurant class, you didn't post it). r is defined by the linq statement (from r) defines r as Restaurant.
    – René Vogt
    May 14, 2019 at 12:31
  • What is the exact error message (and line) you get?
    – René Vogt
    May 14, 2019 at 12:31
  • As rene said, i also tried using this code and it works, i made a mock Restaurant class with a mock enum CuisineType, how are you using this code? The usage is to instanciate a InMemoryRestaurantData and call the get all method and in this case it returns a list with the 3 objects you defined
    – nalnpir
    May 14, 2019 at 12:52
  • Line 29 under r.Name 'List<Restaurant>' does not contain a definition for 'OrderBy' and no accessible extension method 'OrderBy' accepting a first argument of type 'List<Restaurant>' could be found(are you missing a using direct vie or an assembly reference?)
    – S. Curtis
    May 14, 2019 at 13:17
  • 1
    Sounds like you need a reference to 'System.Linq'. At the top of the page with the rest of the using statements, try adding using System.Linq;
    – Craig H
    May 14, 2019 at 13:31

2 Answers 2

5

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

0
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.