简体   繁体   English

C#将Lambda表达式作为方法参数传递

[英]C# Pass Lambda Expression as Method Parameter

I have a lambda expression that I'd like to be able to pass around and reuse. 我有一个lambda表达式,我希望能够传递并重用。 Here's the code: 这是代码:

public List<IJob> getJobs(/* i want to pass the lambda expr in here */) {
  using (SqlConnection connection = new SqlConnection(getConnectionString())) {
    connection.Open();
    return connection.Query<FullTimeJob, Student, FullTimeJob>(sql, 
      (job, student) => {         
        job.Student = student;
        job.StudentId = student.Id;
        return job;
        },
        splitOn: "user_id",
        param: parameters).ToList<IJob>();   
  }   

The key here, is I want to be able to pass the lambda expression that I'm using here into the method that's calling this code, so I can reuse it. 这里的关键是,我希望能够将我在这里使用的lambda表达式传递给调用此代码的方法,因此我可以重用它。 The lambda expression is the second argument inside my .Query method. lambda表达式是我的.Query方法中的第二个参数。 I'm assuming I'd want to use an Action or Func, but I'm not quite sure what the syntax is for this or how it quite works. 我假设我想要使用Action或Func,但我不太确定它的语法是什么或它是如何工作的。 Can someone please give me an example? 有人可以举个例子吗?

Use a Func<T1, T2, TResult> delegate as the parameter type and pass it in to your Query : 使用Func<T1, T2, TResult>委托作为参数类型并将其传递给您的Query

public List<IJob> getJobs(Func<FullTimeJob, Student, FullTimeJob> lambda)
{
  using (SqlConnection connection = new SqlConnection(getConnectionString())) {
    connection.Open();
    return connection.Query<FullTimeJob, Student, FullTimeJob>(sql, 
        lambda,
        splitOn: "user_id",
        param: parameters).ToList<IJob>();   
  }  
}

You would call it: 你会称之为:

getJobs((job, student) => {         
        job.Student = student;
        job.StudentId = student.Id;
        return job;
        });

Or assign the lambda to a variable and pass it in. 或拉姆达分配给一个变量,并通过

If I understand you need following code. 如果我了解您需要以下代码。 (passing expression lambda by parameter) The Method (通过参数传递表达式lambda)方法

public static void Method(Expression<Func<int, bool>> predicate) { 
    int[] number={1,2,3,4,5,6,7,8,9,10};
    var newList = from x in number
                  .Where(predicate.Compile()) //here compile your clausuly
                  select x;
                newList.ToList();//return a new list
    }

Calling method 通话方式

Method(v => v.Equals(1));

You can do the same in their class, see this is example. 你可以在他们的班级做同样的事情,看这是例子。

public string Name {get;set;}

public static List<Class> GetList(Expression<Func<Class, bool>> predicate)
    {
        List<Class> c = new List<Class>();
        c.Add(new Class("name1"));
        c.Add(new Class("name2"));

        var f = from g in c.
                Where (predicate.Compile())
                select g;
        f.ToList();

       return f;
    }

Calling method 通话方式

Class.GetList(c=>c.Name=="yourname");

I hope this is useful 我希望这很有用

Lambda expressions have a type of Action<parameters> (in case they don't return a value) or Func<parameters,return> (in case they have a return value). Lambda表达式有一种Action<parameters> (如果它们不返回值)或Func<parameters,return> (如果它们有返回值)。 In your case you have two input parameters, and you need to return a value, so you should use: 在您的情况下,您有两个输入参数,并且您需要返回一个值,因此您应该使用:

Func<FullTimeJob, Student, FullTimeJob>

You should use a delegate type and specify that as your command parameter. 您应该使用委托类型并将其指定为命令参数。 You could use one of the built in delegate types - Action and Func . 您可以使用其中一种内置委托类型 - ActionFunc

In your case, it looks like your delegate takes two parameters, and returns a result, so you could use Func : 在您的情况下,看起来您的委托接受两个参数,并返回结果,因此您可以使用Func

List<IJob> GetJobs(Func<FullTimeJob, Student, FullTimeJob> projection)

You could then call your GetJobs method passing in a delegate instance. 然后,您可以调用GetJobs方法传递委托实例。 This could be a method which matches that signature, an anonymous delegate, or a lambda expression. 这可以是匹配该签名,匿名委托或lambda表达式的方法。

PS You should use PascalCase for method names - GetJobs , not getJobs . PS你应该使用PascalCase方法名称 - GetJobs ,而不是getJobs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM