c# - Is there any way to pass the lambda expression as a variable or argument? -



c# - Is there any way to pass the lambda expression as a variable or argument? -

i need pass lambda query parameter, followings code sample , interesting find implement it, there samples: thing this:

var expr1 = where(n => n > 6).orderby(n => n % 2 == 0).select(n => n); var expr2 = takewhile((n, index) => n >= index));

and utilize this:

public void uselambda<t> (ienumerable<t> source , lambda expr){ var items= expr.compile(source); foreach(var item in items) console.writeline(item.tostring()); } public void main(){ list<int> numbers = new list<int> { 10, 24, 9, 87, 193, 12, 7, 2, -45, -2, 9 }; var expr1 = where(n => n > 6).orderby(n => n % 2 == 0).select(n => n); uselambda(numbers, expr1); }

does 1 have thought it?

if define linq expressions this:

func<ienumerable<int>, ienumerable<int>> expr1 = l => l.where(n => n > 6).orderby(n => n % 2 == 0).select(n => n); func<ienumerable<int>, ienumerable<int>> expr2 = l => l.takewhile((n, index) => n >= index);

and uselambda method as:

public void uselambda<t> (ienumerable<t> source ,func<ienumerable<t>, ienumerable<t>> lambda) { var items= lambda(source); foreach(var item in items) console.writeline(item.tostring()); } }

then think have you're looking for.

c# .net linq lambda

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -