开发者

c# wpf filter list In Statement

开发者 https://www.devze.com 2023-03-20 14:05 出处:网络
I am working with WPF andc#. I have a list of objects.Example: List<Employee> and the employee class has a property departmentID_A and departmentID_B.

I am working with WPF and c#. I have a list of objects. Example: List<Employee> and the employee class has a property departmentID_A and departmentID_B. The user has 开发者_运维知识库a list of checkbox with the departments name to filter the list. I want to filter the list by the "in statement" something like this: EmployeeList.Find(p => p.departmentID_A in (1, 2, 3)); Is any way that I can do this?


If your filter contains many items you should use

var employees = new List<Employee>();
var filter = new HashSet<int>() { 1, 2, 3, 4 };

var result = from i in employees
             where filter.Contains(i.DepartmentID_B)
             select i;

Note that the LINQ query returns an IEnumerable. It does not filter a list in place. If you want a List or Array you can call ToList() or ToArray() on your result and you may use pure function calls:

var employees = new List<Employee>();
var filter = new HashSet<int>() { 1, 2, 3, 4 };

var result = employees.Where(x => filter.Contains(x.DepartmentID_B));


Try something like this:

        List<Employee> employees = new List<Employee>();
        List<int> filter = new List<int>() { 1, 2, 3, 4 };

        var result = (from i in employees
                      where filter.Contains(i.DepartmentID_B)
                      select i);
0

精彩评论

暂无评论...
验证码 换一张
取 消