尝试通过LINQ将下拉列表填充到C#中的实体

时间:2013-01-17 20:24:52

标签: c# linq entity-framework

我正在尝试填充DropDownlist,但它似乎没有填充,我不知道为什么它不是。我可以得到第二双眼睛来帮助我弄清楚我错在哪里吗?  以下是我的代码:

    private testhousingEntities1 canceldb = new testhousingEntities1();
   /*=================================================*/  
   /*load form                                        */
   /*=================================================*/  
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            InitializeForm();
        }        
    }

    /*================================================*/ 
    /*                                                */
    /*================================================*/  
    private void InitializeForm()
    {
        var version = (from v in canceldb.CancellationVersions
                       where v.Active
                       select v).FirstOrDefault();  

       /*populating states and countries drop downlist */
        FormHelpers.GenerateStates(ddlState);
        FormHelpers.GenerateCountries(ddlCountry);

        /*populating  drop downlist */
        semesterList();            
    }

    /*=================================================*/
    /*populating the Semester drop downlist            */
    /*=================================================*/  
    private void semesterList()
    {
        ddlSemester.Items.Insert(0, new ListItem("---------", "000"));

        var semester = from s in canceldb.cancellationsemesters
                       select new {                                
                                    cid= s.id, cSemester = s.Semester,                           
                                  };
        ddlSemester.DataSource = semester;
        ddlSemester.DataTextField = "cSemester";
        ddlSemester.DataValueField = "cid";
        ddlSemester.DataBind();
    }

    /*=================================================*/  
    /*populating the Semester drop downlist            */
    /*=================================================*/  
    protected void ddlSemester_SelectedIndexChanged(object sender, EventArgs e)
    {              
        if (!IsPostBack)
        {
           semesterList();                
        }
    }

1 个答案:

答案 0 :(得分:1)

尝试添加ToList();

ddlSemester.DataSource = semester.ToList();