I have a legacy class database that is represented by the following model.
public class Course
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public CourseLevel Level { get; set; }
public float FullPrice { get; set; }
public Author Author { get; set; }
public IList<Tag> Tags { get; set; }
public IList<Attendee> Attendees { get; set; }
}
public class Attendee
{
public int Id { get; set; }
public int StudentId { get; set; }
public decimal Tuition { get; set; }
public Student Student { get; set; }
}
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
public class Author
{
public int Id { get; set; }
public string Name { get; set; }
public IList<Course> Courses { get; set; }
}
public class Tag
{
public int Id { get; set; }
public string Name { get; set; }
public IList<Course> Courses { get; set; }
}
I need to get a list of classes that either a part of the course title or description or a part of a student's name matches my search string. The first part is easy.
List<Course> courses = db.Courses.Where(w => w.Title.IndexOf(searchString) > -1 || w.Description.IndexOf(searchString) > -1).ToList();
How do I now filter against w.Attendees.Student.Name?
I tried:
List<Course> courses = db.Courses
.Where(w => w.Title.IndexOf(searchString) > -1 ||
w.Description.IndexOf(searchString) > -1 ||
w.Attendees.Any(a => a.Student.Name.IndexOf(searchString) > -1)).ToList();
And it just returns an empty list.
I'm still kind of new to Linq, I'm coming from Grails. Any help is appreciated.