如何将where语句发送到在LINQ语句中动态执行的方法? [英] How can I send where statements to a method which are dynamically executed in a LINQ statement?

查看:92
本文介绍了如何将where语句发送到在LINQ语句中动态执行的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下示例中, GetFilteredCustomers()可以正常工作,因此我可以发送希望客户使用其姓氏的各种字母.

In the following example, GetFilteredCustomers() works fine so I can send various letters which I want customers to have in their last name.

但是如何构建GetFilteredCustomersDynamic(),使我能够发送完整的where子句,该子句可以动态包含在LINQ语句中?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestDynamicLinq2343
{
    public class Program
    {
        static void Main(string[] args)
        {
            List<Customer> customers = Customer.GetCustomers();

            //semi-dynamic
            foreach (var customer in Customer.GetFilteredCustomers(customers,  "o"))
            {
                Console.WriteLine(customer.LastName);
            }

            //fully-dyanmic (can send where clauses)
            foreach (var customer in Customer.GetFilteredCustomersDynamic(customers, c => c.FirstName.Contains("a")))
            {
                Console.WriteLine(customer.LastName);
            }

            Console.ReadLine();
        }
    }

    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Street { get; set; }
        public string Location { get; set; }
        public string ZipCode { get; set; }

        public static List<Customer> GetCustomers()
        {
            List<Customer> customers = new List<Customer>();
            customers.Add(new Customer { FirstName = "Jim", LastName = "Jones" });
            customers.Add(new Customer { FirstName = "Joe", LastName = "Adams" });
            customers.Add(new Customer { FirstName = "Jake", LastName = "Johnson" });
            return customers;
        }

        public static List<Customer> GetFilteredCustomers(List<Customer> customers, string letter)
        {
            return (from c in customers
                    where c.LastName.Contains(letter)
                    select c).ToList();
        }

        public static List<Customer> GetFilteredCustomersDynamic(List<Customer> customers, Func<..., bool> whereClause)
        {
            return (from c in customers
                    where ...whereClause...
                    select c).ToList();
        }

    }
}

答案:

感谢elder_george和arjuns,我使这个示例可以这样工作(尽管没有Expression<>):

using System;
using System.Collections.Generic;
using System.Linq;

namespace TestDynamicLinq2343
{
    public class Program
    {
        static void Main(string[] args)
        {
            List<Customer> customers = Customer.GetCustomers();

            Func<Customer, bool> whereClause = c => c.LastName.Contains("a") && c.FirstName.Contains("J");

            foreach (var customer in Customer.GetFilteredCustomers(customers, whereClause))
            {
                Console.WriteLine(customer.LastName);
            }

            Console.ReadLine();
        }
    }

    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Street { get; set; }
        public string Location { get; set; }
        public string ZipCode { get; set; }

        public static List<Customer> GetCustomers()
        {
            List<Customer> customers = new List<Customer>();
            customers.Add(new Customer { FirstName = "Jim", LastName = "Jones" });
            customers.Add(new Customer { FirstName = "Joe", LastName = "Adams" });
            customers.Add(new Customer { FirstName = "Jake", LastName = "Johnson" });
            customers.Add(new Customer { FirstName = "Angie", LastName = "Reckar" });
            customers.Add(new Customer { FirstName = "Jean-Luc", LastName = "Beaudoir" });
            return customers;
        }

        public static List<Customer> GetFilteredCustomers(List<Customer> customers, Func<Customer, bool> whereClause)
        {
            return customers
                   .Where(whereClause).ToList();
        }

    }
}

推荐答案

您需要将过滤器表示为Expression<Func<Customer, bool>>,而不是Func<Customer, bool>.这样,您可以使用Queryable.Where方法将此过滤器添加到LINQ表达式树中.

You'd need to represent filter as Expression<Func<Customer, bool>>, not as Func<Customer, bool>. This way you can use Queryable.Where method to add this filter to LINQ expression tree.

我错了,因为此代码将LINQ应用于对象,而委托是适当的过滤条件.我的错.

I was wrong, as this code uses LINQ to objects, where delegates are proper filter criteria. My bad.

例如(已更正为使用普通委托):

E.g. (corrected to using normal delegates):

    public static List<Customer> GetFilteredCustomersDynamic(List<Customer> customers, Func<Customer, bool> whereClause)
    {
        return customers
               .Where(whereClause).ToList();
    }

    public static List<Customer> GetFilteredCustomers(List<Customer> customers, string letter)
    {
        return GetFilteredCustomersDynamic(c => c.LastName.Contains(letter));
    }

这篇关于如何将where语句发送到在LINQ语句中动态执行的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆