0

如果选择了一个或多个特定复选框列表项,我正在尝试找到正确的代码隐藏以显示列表框。复选框列表是从实体数据源创建的,并且该源的 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 New Course Request

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

有人可以帮忙吗?

4

3 回答 3

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 字段。我有 ' select g ' 应该有 '*select g.grade_level_id*' (这是 Guid 字段)。不确定我是否需要 ToList ...

于 2012-06-21T22:41:33.067 回答
0

这应该可以解决问题(对 .Where(...) 答案稍作修改):

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

于 2012-06-21T22:44:33.360 回答
0

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

if(guids.Where(x=>x==new Guid(grade.Value)).FirstOrDefault()!=null)
{
     //etc
}
于 2012-06-21T21:18:27.580 回答