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 Filtering a Query

Eric Lepage
Eric Lepage
1,602 Points

Anyone has the solution for Entity Framework Basic challenge Repository.GetCoursesByTeacher()

I can not get to write the appropriate code to get the answer right

Eric Lepage
Eric Lepage
1,602 Points

oops, just saw this post which seems to be covering the same problem I'm having, although after a first read, I don't get the answer...

https://teamtreehouse.com/community/linq-query-syntax-where-and-include

Steven Parker
Steven Parker
221,293 Points

The other issue might not be the same as yours. Please show your code so we can take a look.

Eric Lepage
Eric Lepage
1,602 Points

Hello Steven, thanks for the quick reply!

My code: var Courses = context.Courses .Include(cb => Teacher) .Where(cb.Teacher.LastName == lastName) .ToList();

Will give me these errors: Repository.cs(22,40): error CS0118: Treehouse.CodeChallenges.Teacher' is atype' but a variable' was expected Repository.cs(22,40): error CS0119: Expression denotes atype', where a variable',value' or method group' was expected Repository.cs(22,34): error CS1660: Cannot convertlambda expression' to non-delegate type `string' Compilation failed: 3 error(s), 0 warnings

The last example in James Churchill ends with using this code: var comicBooks = context.ComicBooks .Include(cb => cb.Series) .Where(cb => cb.IssueNumber == 1 || cb.Series.Title == "The Amazing Spider-Man") .ToList();

When I look at the ComicBook and Series class, they seem similar in their relation as the Course and Teacher classes.

I know I can solve the problem using this code that I found in another thread: var Courses = context.Courses .Where(cb => cb.Teacher.LastName == lastName) .ToList(); return Courses;

But I would really like an explanation on why I get the errors if I use .Include since it seems similar to the example in the video.

2 Answers

Steven Parker
Steven Parker
221,293 Points

You don't need .Include for this challenge, but using it isn't the issue, it's the lambda syntax. You're missing parts of it in both the .Include and the .Where calls:

         var Courses = context.Courses.Include(cb => cb.Teacher)
//                                                   ^^^  missing part
                              .Where(cb => cb.Teacher.LastName == lastName).ToList();
//                                   ^^^^^  missing part
Eric Lepage
Eric Lepage
1,602 Points

Thanks Steven! I was missing out a very important detail!