如何将此linq查询与组合框组合

时间:2015-03-08 08:52:39

标签: c# combobox linq-to-entities

我有两个具有CourseID和Name的表课程,以及具有SectionID,CourseID,价格,时间的部分。我有以下代码,我不知道如何使用组合框的desiplaymember和displayvalue。 提前谢谢

var fillcmb=(from crs in re.Sections
             from r in re.Courses
                where crs.CourseID == r.CourseID
                select r.Name.ToString()).ToList();

        coursecmb.DataSource = fillcmb;
        coursecmb.DisplayMember = "Name";
        coursecmb.ValueMember = "CourseID";

1 个答案:

答案 0 :(得分:1)

您没有从LINQ查询中选择适当的属性:

var fillcmb=(from crs in re.Sections
             from r in re.Courses
                where crs.CourseID == r.CourseID
                select new { Name = r.Name.ToString(), CourseID = crs.CourseID})
            .ToList();