Create LINQ Query With Multiple Filters on a Single Column And Multiple Columns with Filters

I have a dataset that I need to filter on multiple columns (six in total). Not every column will be filtered, i.e. a user can filter on column 1 and 3, but not 2,4,5, or 6. I can filter successfully when there is only a single value in each column. However once a column has multiple filters it fails.

Use a scenario where I want to filter on multiple POCs

Here is my code:

IEnumerable<IssueDataModel> filteredIssues = issueList;//Passed in dataset

var pocitemsArray = Array.Empty<string>();
var notificationitemsArray = Array.Empty<string>();

if (filterColumnPOCs == "Y")//This indicates the user has selected the POC Filter
{
    pocitemsArray = filterItemsPOCs.Split(';');//This is the string of users to filter on
    foreach (string currentItem in pocitemsArray)//Checking for each POC
    {
        filteredIssues = filteredIssues.Where(s => s.PocName1.Contains(currentItem) || s.originatorPocName.Contains(currentItem));
    }
}

if (filterColumnNotification == "Y")
{
    notificationitemsArray = filterItemsNotification.Split(',');
    foreach (string currentItem in notificationitemsArray)
    {
        filteredIssues = filteredIssues.Where(s => s.notificationType.Contains(currentItem));
    }
}

The issue is that when I filter the first POC, the second pass thru there won’t be any matches for the second POC, so there are no records returned.

If I try matching on the initial dataset (issuelist) it works, but then I can’t cascade one filter to the next filter. I.e filter the dataset for POC and then take that filtered dataset to Notification for filtering. Keep in mind have have 6 total filter loops but not all may be used at the same time.

The problem is that you’re using individual Wheres for all values in each filter: consecutive Where statements are equivalent to doing condition1 && condition2

In effect, the column would have to have all values at the same time in order to not get removed. This is why you’re getting results for when there’s one possible value in the filter, but not when there’s multiple values in the filter. It’d be equivalent to if (x == values[0] && x == values[1])

You can instead rewrite the filtering to use one Where, and Any to match against any possible value, instead of using a Where per value:

IEnumerable<IssueDataModel> filteredIssues = issueList;//Passed in dataset

var pocitemsArray = Array.Empty<string>();
var notificationitemsArray = Array.Empty<string>();

if (filterColumnPOCs == "Y")//This indicates the user has selected the POC Filter
{
    pocitemsArray = filterItemsPOCs.Split(';');//This is the string of users to filter on

    filteredIssues = filteredIssues.Where(issue =>              // For every issue
        pocitemsArray.Any(filterValue =>                        // For ANY value in the filter
            issue.PocName1.Contains(filterValue)                // Match
            || issue.originatorPocName.Contains(filterValue)));
}

if (filterColumnNotification == "Y")
{
    notificationitemsArray = filterItemsNotification.Split(',');

    filteredIssues = filteredIssues.Where(issue =>              // For every issue
        notificationitemsArray.Any(filterValue =>               // For ANY value in the filter
            issue.notificationType.Contains(filterValue)));     // Match
}

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật