如果选中的复选框列表项目在linq结果中,则显示列表框

时间:2012-06-21 20:35:14

标签: c# asp.net linq

如果选择了一个或多个特定的复选框列表项,我正在尝试找到正确的代码隐藏以显示列表框。复选框列表是从实体数据源创建的,该源的SQL表还有一个字段,指示该选择是否应显示列表框。呼!该表如下所示:

GUID grade_level show_college_list
(gen)    12           0 (bit)
(gen)  College        1

asp就是这样:

        <asp:EntityDataSource ID="GradeLevelEntityDataSource" runat="server" 
            ConnectionString="name=NewCourseRequestDataEntities" 
            DefaultContainerName="NewCourseRequestDataEntities" EnableFlattening="False" 
            EntitySetName="grade_levels" OrderBy="it.grade_level_description">
        </asp:EntityDataSource>
        <asp:Label ID="Label7" cssClass="leftlabel" runat="server" text="Grade Level (check all that apply):"></asp:Label>
        <asp:CheckBoxList ID="GradeLevelCheckBoxList" runat="server" cssClass="horizontalcontrols"
            DataSourceID="GradeLevelEntityDataSource" 
            DataTextField="grade_level_description" DataValueField="grade_level_id" AutoPostBack="True"
            OnSelectedIndexChanged="CollegeInstitutionsListboxChange"
            RepeatDirection="Horizontal" RepeatLayout="Flow">
        </asp:CheckBoxList>

如果选中College,我希望显示机构列表框面板。 这就是我对代码的了解:

       if (sender != null)
            {
                foreach (ListItem grade in GradeLevelCheckBoxList.Items)
                {
                    if (grade.Selected == true)
                    {
                        NewCourseRequestDataEntities context = new NewCourseRequestDataEntities();
                        var guids = from g in context.grade_levels where g.show_college_list == true select g;

                        if ( guids.Contains(new Guid(grade.Value)) )
                        {
                            testselected.Text = grade.Value; //for testing
                            CollegeInstitutionsSelectPanel.Visible = true;
                        }
                    }
                }
            }         

我在contains方法上收到了这两个错误。我不知道他们的意思。

错误1实例参数:无法从'System.Linq.IQueryable'转换为'System.Linq.ParallelQuery'Y:\ visual studio \ New Course Request \ NewCourseRequestForm.aspx.cs 146 34新课程申请

错误2'System.Linq.IQueryable'不包含'Contains'的定义和最佳扩展方法重载'System.Linq.ParallelEnumerable.Contains(System.Linq.ParallelQuery,TSource)'有一些无效的参数Y :\ visual studio \ New Course Request \ NewCourseRequestForm.aspx.cs 146 34新课程申请

有人可以帮忙吗?

3 个答案:

答案 0 :(得分:0)

尝试(对于Error2-我不明白Error1来自哪里......):

if(guids.Where(x=>x==new Guid(grade.Value)).FirstOrDefault()!=null)
{
     //etc
}

答案 1 :(得分:0)

找到它 -

     NewCourseRequestDataEntities context = new NewCourseRequestDataEntities();
     var guids = (from g in context.grade_levels where g.show_college_list == true select g**.grade_level_id**).ToList();

                        bool contains = guids.Contains(new Guid(grade.Value));
                        if (contains)
                        {
                            CollegeInstitutionsSelectPanel.Visible = true;
                        }
                        else
                        {
                            CollegeInstitutionsSelectPanel.Visible = false;
                        }

我没有在表格中指定Guid字段。我有'选择g '应该有'* select g.grade_level_id *'(这是Guid字段)。不确定我是否需要ToList ...

答案 2 :(得分:0)

这应该可以解决问题(稍微修改.Where(...)回答):

foo.Visible = guids.Any(x=>x==new Guid(grade.Value)