Linq to 对象过滤 IN 和 not in

发布于 2024-12-10 11:47:09 字数 3579 浏览 0 评论 0原文

寻找一个示例,我可以根据某些过滤条件过滤我的集合。

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

在下面的示例中,在我的 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; }
        }

Looking for an example where I can filter my collection based on some filtering criteria.

I have been looking for some example where given a list /array i can filter a collection.

In the example below in my find method I am trying to filter based on 2 values ,looking for something like an "IN" function any suggestions?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

静若繁花 2024-12-17 11:47:09
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 类不应该使用相同的值吗?而不是字符串

如果将 criteria.Departments 定义为 List 那么您可以编写

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

But considering you use an enum for the Department (DepartmentType), shouldn't your Criteria class use the same instead of a string?

If you define the criteria.Departments as List<DepartmentType> then you can write

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();
}
小…红帽 2024-12-17 11:47:09

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

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

Contains returns a bool defining whether a specified object is contained in a collection. Based on your example, you will need to use Where to filter the customers, then use Contains on the departments:

 customers = customers.Where(c => criteria.Departments.Contains(c.Department));
伪心 2024-12-17 11:47:09

我想你想要这样的东西..

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

i think you want something like this..

customers = customers.Where(c => criteria.Departments.Contains(c.Department)); 
指尖上得阳光 2024-12-17 11:47:09

你想要

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

You want

Customers.Where(c => criteria.Departments.Contains(c.Department.ToString()))
雨落星ぅ辰 2024-12-17 11:47:09

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

    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 

我还没有对此进行测试,但我认为它应该可以工作并带回您想要的内容。

Not sure if this is what you're looking for but the following:

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

Would equate to something like this in SQL:

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

I haven't tested this but I think it should work and bring back what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文