Expression<Func<T,TResult>>和Func<T,TResult>

简介: 1.Expression是表达式 //使用LambdaExpression构建表达式树 Expression expr = (x, y, z) => (x + y) / z; Console.

1.Expression<Func<T,TResult>>是表达式

//使用LambdaExpression构建表达式树
            Expression<Func<int, int, int, int>> expr = (x, y, z) => (x + y) / z;
            Console.WriteLine(expr.Compile()(1, 2, 3));

https://msdn.microsoft.com/zh-cn/library/system.linq.expressions.expression(v=vs.100).aspx

https://msdn.microsoft.com/zh-cn/library/bb335710(v=vs.100).aspx

 

2.Func<T, TResult> 委托

封装一个具有一个参数并返回 TResult 参数指定的类型值的方法。
public delegate TResult Func<in T, out TResult>(T arg)
类型参数
in T
此委托封装的方法的参数类型。
该类型参数是逆变的。即可以使用指定的类型或派生程度更低的类型。有关协变和逆变的更多信息,请参见泛型中的协变和逆变。
out TResult
此委托封装的方法的返回值类型。
该类型参数是协变的。即可以使用指定的类型或派生程度更高的类型。有关协变和逆变的更多信息,请参见泛型中的协变和逆变。
参数
arg
类型:T
此委托封装的方法的参数。
返回值
类型:TResult
此委托封装的方法的返回值。

  string mid = ",middle part,";
            ///匿名写法
            Func<string, string> anonDel = delegate(string param)
            {
                param += mid;
                param += " And this was added to the string.";
                return param;
            };
            ///λ表达式写法
            Func<string, string> lambda = param =>
                {
                    param += mid;
                    param += " And this was added to the string.";
                    return param;
                };
            ///λ表达式写法(整形)
            Func<int, int> lambdaint = paramint =>
                {
                    paramint = 5;
                    return paramint;
                };
            ///λ表达式带有两个参数的写法
            Func<int, int, int> twoParams = (x, y) =>
                {
                  return  x*y;
                };
            MessageBox.Show("匿名方法:"+anonDel("Start of string"));
            MessageBox.Show("λ表达式写法:" + lambda("Lambda expression"));
            MessageBox.Show("λ表达式写法(整形):" + lambdaint(4).ToString());
            MessageBox.Show("λ表达式带有两个参数:" + twoParams(10, 20).ToString());

 来自:

http://blog.csdn.net/shuyizhi/article/details/6598013

3.使用Expression进行查询拼接

    public static class DynamicLinqExpressions
     {
 
         public static Expression<Func<T, bool>> True<T>() { return f => true; }
         public static Expression<Func<T, bool>> False<T>() { return f => false; }
 
         public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1,
                                                             Expression<Func<T, bool>> expr2)
         {
             var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
             return Expression.Lambda<Func<T, bool>>
                   (Expression.Or(expr1.Body, invokedExpr), expr1.Parameters);
         }
 
         public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1,
                                                              Expression<Func<T, bool>> expr2)
         {
             var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
             return Expression.Lambda<Func<T, bool>>
                   (Expression.And(expr1.Body, invokedExpr), expr1.Parameters);
         }
 
     }

使用方法

       public static IEnumerable<T> FilterBy<T>(this IEnumerable<T> collection, IEnumerable<KeyValuePair<string, string>> query)
         {
             var filtersCount = int.Parse(query.SingleOrDefault(k => k.Key.Contains("filterscount")).Value);
             
             Expression<Func<T, bool>> finalquery = null;
 
             for (var i = 0; i < filtersCount; i += 1)
             {
                 var filterValue = query.SingleOrDefault(k => k.Key.Contains("filtervalue" + i)).Value;
                 var filterCondition = query.SingleOrDefault(k => k.Key.Contains("filtercondition" + i)).Value;
                 var filterDataField = query.SingleOrDefault(k => k.Key.Contains("filterdatafield" + i)).Value;
                 var filterOperator = query.SingleOrDefault(k => k.Key.Contains("filteroperator" + i)).Value;
 
                 Expression<Func<T, bool>> current = n => GetQueryCondition(n, filterCondition, filterDataField, filterValue);
 
                 if (finalquery == null)
                 {
                     finalquery = current;
                 }
                 else if (filterOperator == "1")
                 {
                     finalquery = finalquery.Or(current);
                 }
                 else
                 {
                     finalquery = finalquery.And(current);
                 }
             };
 
             if (finalquery != null)
                 collection = collection.AsQueryable().Where(finalquery);
 
             return collection;
         }

要使用AsQueryable,必须引入 LinqKit.EntityFramework;如果不使用AsQueryable将会报错。

 

原文:http://blog.csdn.net/nabila/article/details/8137169

 

相关文章
|
9月前
|
C#
对C#中set{} get{}的一点理解
对C#中set{} get{}的一点理解
|
10月前
|
Dart
Dart报The return type ‘bool‘ isn‘t a ‘void‘, as required by the closure‘s context
Dart报The return type ‘bool‘ isn‘t a ‘void‘, as required by the closure‘s context
|
Linux
编译OpenJDK8:error: control reaches end of non-void function [-Werror=return-type]
编译OpenJDK8:error: control reaches end of non-void function [-Werror=return-type]
153 0
TypeError: sequence item 0: expected string, int found
TypeError: sequence item 0: expected string, int found
|
C++
一个函数两个return
一个函数两个return
147 0
|
Python
AttributeError: 'module' object has no attribute 'main'
AttributeError: 'module' object has no attribute 'main'
156 0
AttributeError: 'module' object has no attribute 'main'
|
JavaScript
认识 Express 的 res.send() 和 res.end()
在使用 Node.js 的服务端代码中,如果使用的是 Express 框架,那么对于一个请求,常常会有两种响应方式:
333 0
认识 Express 的 res.send() 和 res.end()
成功解决AttributeError: module 'string' has no attribute 'find'
成功解决AttributeError: module 'string' has no attribute 'find'
Lambda forEach 关于 return 的使用
版权声明:本文首发 http://asing1elife.com ,转载请注明出处。 https://blog.csdn.net/asing1elife/article/details/82655640 ...
1610 0