C# Entity Framework Basics LINQ Queries Filtering a Query

olu adesina
PRO
olu adesina
Pro Student 19,018 Points

linq query syntax where() and include()

where im i going wrong

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.ToList();
            }
        }

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



            }
        }
    }
}
Teacher.cs
namespace Treehouse.CodeChallenges
{
    public class Teacher
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

1 Answer

Steven Parker
Steven Parker
171,227 Points

You won't need to use .Include() here since "Teacher" is a property of a "Courses" record.

But you will need to expand the .Where() argument to make it into a predicate function.