列表上的条件筛选器

时间:2014-09-22 17:15:31

标签: c# linq

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语句过滤后,我绑定到网格 - 但它始终是相同的记录数 - 几乎就像没有应用过滤器一样。为什么dbRecords没有被Where语句调整?

1 个答案:

答案 0 :(得分:3)

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