使用LINQ根据子集合选择记录项

时间:2015-03-20 11:10:30

标签: c# linq linq-to-entities

我有讲师班。

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; }
}

我想按照他教的名字和课程搜索教练 我使用谓词来建立我的条件 但我需要访问教师内部的课程1集合,以获得教授本课程的教师,但我不能

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
        }

1 个答案:

答案 0 :(得分:2)

Courses1没有任何ID属性。试着这样做:

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