列表上的条件筛选器

本文关键字:筛选 条件 列表 | 更新日期: 2023-09-27 18:23:52

var dbRecords = (from c ...... query removed).Where(c => c.BNumber.Contains(bnum)).ToList();
// At this point dbRecords contains 596 records
int chkCount = 0;
// I have a series of checkboxes for filtering of the query
foreach (ListItem c in chklColumns.Items)
{
    if (c.Selected == true)
    {
        chkCount++;
    }
    if (chkCount > 0)
    {
        foreach (ListItem x in chklColumns.Items)
        {
            var gridKey = "Grp" + x.Value;
            if (x.Selected)
            {
               if (gridKey == "GrpSOS")
               {
                   dbRecords.Where(p => p.SOSEnabledYN == true);
               }
               if (gridKey == "GrpMOB")
               {
                   dbRecords.Where(p => p.ZMobileEnabledYN == true);
                   // If this filter were applied then my resultset would be 276 records
                   // I step through and hit this code but it does not seem to be affecting the dbRecords list
               }
               if (gridKey == "GrpPHO")
               {
                   dbRecords.Where(p => p.PhoneMonitorEnabledYN == true);
               }
           }
       }
   }
WebDataGrid1.DataSource = dbRecords;
WebDataGrid1.DataBind();

使用每个复选框的 Where 语句进行过滤后,我绑定到网格 - 但它始终是相同的记录计数 - 几乎就像没有应用过滤器一样。 为什么 Where 语句没有调整数据库记录?

列表上的条件筛选器

Where()返回一个新的过滤列表,它不会修改原始列表,因此您需要像这样存储过滤列表:

dbRecords = dbRecords.Where(p => p.SOSEnabledYN == true);

例:

List<User> myUsers = New List<User>();
//fill the list myUsers with some User object
List<User> activeUsers = myUsers.Where(u => u.Active == True);
//the activeUsers list will contain only active users filtered from list of all users