C# Entity Framework Basics LINQ Queries Writing a List Query

Instantiate an instance of

HELP!!!!!!!!

Repository.cs
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Diagnostics;


namespace Treehouse.CodeChallenges
{
    public static class Repository
    {
        public static List<Course> GetCourses()
        {
            using (var context = new Context())
            {

                  context.Database.Log = (message) => Debug.WriteLine(message);
                  var Context = context.Course.Tolist();
                  console.WriteLine("# of courses: {0}", course.Count);
            }
        }
    }
}

1 Answer

Vicky Lien
Vicky Lien
2,880 Points
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;

namespace Treehouse.CodeChallenges
{
    public static class Repository
    {
        public static List<Course> GetCourses()
        {
            //Instantiate an instance of the Context class within a using statement
            using(var context = new Context())              
            {
                // Return the results of calling ToList on the context's Courses DbSet property
                return context.Courses.ToList();
            }
        }
    }
}

Let's dissect the code and see what we are doing. We instantiate an instance of the Context class by writing:

var context = new Context()

and given the new instance variable the name "context".

Now, based on the Code Challenge description, we know that the context instance should have a Courses DbSet property. So, we can access the property by writing:

context.Courses

Then, the Code Challenge description told us that we need to call ToList on the property. So, we call the ToList function by:

context.Courses.ToList()

The very last step is to return the result. We can do so by using the return statement:

return context.Courses.ToList();