Engy Roshdy Louis
Engy Roshdy Louis

Reputation: 15

Using LINQ To Select Records Based on Child Collections item

i have instructor class.

public partial class Instructor
{
    public Instructor()
    {
        this.Courses = new HashSet<Course>();
        this.Courses1 = new HashSet<Course>();
    }

    public int ID { get; set; }
    public string FName { get; set; }
    public string LName { get; set; }
    public int UserID { get; set; }

    public virtual ICollection<Course> Courses { get; set; }
    public virtual User User { get; set; }
    public virtual ICollection<Course> Courses1 { get; set; }
}

i want to search for instructor by name and course that he teaches i used predicate to build my condition but i need to access courses 1 collection inside instructor to get instructors whom teach this course but i can't

var Predict = PredicateBuilder.True<Instructor>();

        if (chkNameSearch.Checked)
        {
            Predict = Predict.And(ins => ins.FName == txtNameSearch.Text);
        }

        if (chkCourseSearch.Checked)
        {

            int CourseID=int.Parse(cmbCourseSearch.SelectedValue.ToString());
            Predict = Predict.And(ins => ins.Courses1.ID == CourseID);  //Error
        }

Upvotes: 0

Views: 355

Answers (1)

Juan
Juan

Reputation: 3705

Courses1 doens't have any ID property. Try doing this:

Predict = Predict.And(ins => ins.Courses1.Any(c => c.ID == CourseID));

Upvotes: 2

Related Questions