C# Pass Lambda Expression as Method Parameter

后端 未结 4 697
故里飘歌
故里飘歌 2020-11-29 00:21

I have a lambda expression that I\'d like to be able to pass around and reuse. Here\'s the code:

public List getJobs(/* i want to pass the lambd         


        
相关标签:
4条回答
  • 2020-11-29 00:43

    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). In your case you have two input parameters, and you need to return a value, so you should use:

    Func<FullTimeJob, Student, FullTimeJob>
    
    0 讨论(0)
  • 2020-11-29 00:46

    Use a Func<T1, T2, TResult> delegate as the parameter type and pass it in to your 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.

    0 讨论(0)
  • 2020-11-29 00:48

    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.

    In your case, it looks like your delegate takes two parameters, and returns a result, so you could use Func:

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

    You could then call your GetJobs method passing in a delegate instance. This could be a method which matches that signature, an anonymous delegate, or a lambda expression.

    P.S. You should use PascalCase for method names - GetJobs, not getJobs.

    0 讨论(0)
  • 2020-11-29 00:49

    If I understand you need following code. (passing expression lambda by parameter) The Method

    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

    0 讨论(0)
提交回复
热议问题