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 Detail Query

problems understanding the question

Instantiate an instance of the Context class within a using statement

I dont know if this begins in the top of the page with the other using statements

Use the Include operator to eagerly load the course's related teacher entity

does this go in the area where the green comment is ?

Use the SingleOrDefault LINQ operator to filter the available courses to the course whose Id property equals the passed in id parameter value

can anyone give an example of they mean by this last statement. I dont understand how to interpret

Course.cs
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace Treehouse.CodeChallenges
{
    public class Course
    {
        public Course()
        {
            Students = new List<CourseStudent>();
        }

        public int Id { get; set; }
        public int TeacherId { get; set; }
        [Required, StringLength(200)]
        public string Title { get; set; }
        public string Description { get; set; }
        public int Length { get; set; }

        public Teacher Teacher { get; set; }
        public ICollection<CourseStudent> Students { get; set; }
    }
}
Repository.cs
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;

namespace Treehouse.CodeChallenges
{
    public static class Repository
    {
        public static List<Course> GetCourses()
        {
            using (var context = new Context())
            {
                return context.Courses
                              .OrderBy(c => c.Teacher.LastName)
                              .ThenBy(c => c.Teacher.FirstName)
                              .ToList();
            }
        }

        public static List<Course> GetCoursesByTeacher(string lastName)
        {
            using (var context = new Context())
            {
                return context.Courses
                              .Where(c => c.Teacher.LastName == lastName)
                              .ToList();
            }
        }

        public static Course GetCourse(int id)
        {
            return null; 

            using(var context = new Context())
{
    return context.Courses.Where(o => o.Teachers.Equals(lastName)).ToList();
}
        }
    }
}

4 Answers

Steven Parker
Steven Parker
231,198 Points

Remember, there are two kinds of "using statements".

Look in Repository.cs. The top 3 lines are one type, but on line 11 you will see an example of the other type. It's that second type that the challenge is talking about.

And you can use SingleOrDefault similarly to Where, by giving it a function parameter that filters the enumerable using some condition. This might be a good place for a lambda expression. There's an example of a Where with a lambda on line 25.

using System.Collections.Generic; using System.Data.Entity; using System.Linq;

namespace Treehouse.CodeChallenges { public static class Repository { public static List<Course> GetCourses() { using (var context = new Context()) { return context.Courses .OrderBy(c => c.Teacher.LastName) .ThenBy(c => c.Teacher.FirstName) .ToList(); } }

    public static List<Course> GetCoursesByTeacher(string lastName)
    {
        using (var context = new Context())
        {
            return context.Courses
                          .Where(c => c.Teacher.LastName == lastName)
                          .ToList();
        }
    }

    public static Course GetCourse(int id)
    {
        using (var context = new Context())
        {
        return context.Courses
                          .Where(c => c.Id == id)
                          .Include( c => c.Teacher.Select(t => t.Teacher ))
                          .SingleOrDefault();
        }
    }
}

}

using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;

namespace Treehouse.CodeChallenges
{
    public static class Repository
    {
        public static List<Course> GetCourses()
        {
            using (var context = new Context())
            {
                return context.Courses
                              .OrderBy(c => c.Teacher.LastName)
                              .ThenBy(c => c.Teacher.FirstName)
                              .ToList();
            }
        }

        public static List<Course> GetCoursesByTeacher(string lastName)
        {
            using (var context = new Context())
            {
                return context.Courses
                              .Where(c => c.Teacher.LastName == lastName)
                              .ToList();
            }
        }

        public static Course GetCourse(int id)
        {
            using (var context = new Context())
            {
                return context.Courses
                    .Include("Teacher")
                    .SingleOrDefault(c => c.Id == id);
            }
        }
    }
}

looking for an answer to this one

Steven Parker
Steven Parker
231,198 Points

If the hints given to Amy don't address your issue, you might start your own question. Be sure to include your properly formatted code and a link the course page (both are usually done for you if you use the "get help" button).

public static Course GetCourse(int id) { using (var context = new Context()) { return context.Courses .Include( "Teacher") .SingleOrDefault(c => c.Id == id); } }

Thanks for all replies. Appreciated it.