Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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();