Linq到对象过滤IN而不是

时间:2011-10-16 16:03:08

标签: c# linq-to-objects

寻找一个示例,我可以根据一些过滤条件过滤我的收藏。

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

在我的find方法中的下面示例中,我尝试根据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; }
        }

5 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:2)

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();
}

但考虑到enum使用DepartmentDepartmentType),您的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();
}

答案 2 :(得分:1)

你想要

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

答案 3 :(得分:1)

我想你想要这样的事情......

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

答案 4 :(得分:0)

不确定这是否是您正在寻找的内容,但以下内容:

    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 

我没有对此进行测试,但我认为它应该可以工作并带回你想要的东西。