链接到过滤IN而不是IN的对象

本文关键字:IN 对象 过滤 链接 | 更新日期: 2023-09-27 18:11:18

寻找一个示例,我可以根据一些过滤标准过滤我的集合。

我一直在寻找一些例子,其中给出一个列表/数组,我可以过滤一个集合。

在下面的示例中,在我的查找方法中,我试图根据2个值进行过滤,寻找类似于"In"函数的东西,有任何建议吗?

        class Program
        {
            static void Main()
            {
                //Print all customres that belong to below deparments and match on surname
                var criteria=new Criteria
                                 {
                                     Departments = new List<string> {"BusinessAnalyst", "Account"},
                                     Surname = "Bloggs"
                                 };
                List<Customer> customers = Repository.Find(criteria);
                customers.ForEach(x => Console.WriteLine(string.Format("Surname: {0} Department :{1}", x.Surname,x.Department)));
                Console.Read();
            }
        }
        public class Repository
        {
           public static List<Customer>GetCustomers()
            {
                return  new List<Customer>
                                    {
                                        new Customer { Name = "Jon",Surname="Smith",Department = DepartmentType.Managers},
                                        new Customer{Name = "Bill",Surname = "Gates",Department = DepartmentType.Managers},
                                        new Customer { Name = "Mary",Surname = "Bug",Department = DepartmentType.Developers},
                                        new Customer { Name = "Mark",Surname="Boo",Department = DepartmentType.Account},
                                        new Customer{Name = "Ron",Surname = "Scott",Department = DepartmentType.Managers},
                                        new Customer { Name = "Jonny",Surname = "Dip",Department = DepartmentType.Developers},
                                        new Customer { Name = "Mary",Surname = "Bloggs",Department = DepartmentType.BusinessAnalyst},
                                        new Customer { Name = "Mary",Surname = "Bug",Department = DepartmentType.Account},
                                        new Customer { Name = "Jonny",Surname = "Dip",Department = DepartmentType.Account},
                                        new Customer { Name = "Mary",Surname = "Bloggs",Department = DepartmentType.Managers}
                                    };
            }
           public static List<Customer> Find(Criteria criteria)
           {
               List<Customer>customers=Repository.GetCustomers();
               //Filter on departments
               //ERROR HERE AS I cannot do this "IN" would be fantastic.
               customers = customers.Contains(criteria.Departments);
               //now filter on name
               customers = customers.Where(x => x.Surname == criteria.Surname).ToList();

               return customers;
           }
        }
        public enum DepartmentType
        {
            Account,
            Managers,
            Developers,
            BusinessAnalyst
        }
        public class Customer
        {
            public string Name { get; set; }
            public string Surname { get; set; }
            public DepartmentType Department { get; set; }
        }
        public class Criteria
        {
            public Criteria()
            {
                Departments=new List<string>();
            }
            public string Name { get; set; }
            public string Surname { get; set; }
            public List<string> Departments { get; set; }
        }

链接到过滤IN而不是IN的对象

public static List<Customer> Find(Criteria criteria)
{
    List<Customer> customers = Repository.GetCustomers();
    var customers2 = customers.Where(x => criteria.Departments.Contains(x.Department.ToString()));
    var customers3 = customers2.Where(x => x.Surname == criteria.Surname);
    return customers3.ToList();
}

但是考虑到您为Department (DepartmentType)使用enum,您的Criteria类不应该使用相同的而不是string吗?

如果你将criteria.Departments定义为List<DepartmentType>,那么你可以写

public static List<Customer> Find(Criteria criteria)
{
    List<Customer> customers = Repository.GetCustomers();
    var customers2 = customers.Where(x => criteria.Departments.Contains(x.Department));
    var customers3 = customers2.Where(x => x.Surname == criteria.Surname);
    return customers3.ToList();
}

Contains返回一个bool值,定义指定对象是否包含在集合中。根据您的示例,您需要使用Where来过滤客户,然后在部门上使用Contains:

 customers = customers.Where(c => criteria.Departments.Contains(c.Department));

我想你需要这样的东西。

customers = customers.Where(c => criteria.Departments.Contains(c.Department)); 

你想

Customers.Where(c => criteria.Departments.Contains(c.Department.ToString()))

不确定这是否是你要找的,但以下内容:

    List<Customer> FilteredCustomers = (from c in customers where Criteria.Departments.Contains(c.deparment) && c.surname == Criteria.Surname select c).ToList();

在SQL中相当于:

    SELECT * 
    FROM Customers
    WHERE Department IN (
    List of departments
    ) 
    AND Surname = surname 

我还没有测试过这个,但我认为它应该工作,并带回你想要的