Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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