lambda expressions

lambda:

Java lambda expression is new in Java 8 .Java lambda expression is the first step into the Java functional programming. Therefore, Java lambda expression is a function that can be created under does not belong to any class of cases. Java lambda expression can be passed as an object, and performed as required.

Java lambda expression is commonly used to implement simple event listeners / callbacks, or using Java Streams API conduct functional programming .

 

lambda expressions use:

Many times when we query using Lambda expressions, such as using Lambda expressions to query the user data, sometimes the user to query information by telephone or mail, user name and sometimes will go to query the user information

    var user = db.Set<U_User>().Where(c => c.UserName = "nee32");
    var user = db.Set<U_User>().Where(c => c.TelePhone = "13888888888");

In fact, results of the query are the same, but the only difference is that Lambda expressions conditions are not the same, so can not just write a query method, and the realization Lambda expressions Where are query it? The answer of course is able! For example, a method using a plurality of three-tier architecture satisfies a query condition, as follows

    public class UserDAL
    {
        /// <summary>
        /// 根据条件查找用户列表
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public List<U_User> FindAll(System.Linq.Expressions.Func<U_User, bool>> where = null)
        {
            using (EFContext db = new EFContext())
            {
                if (where == null)
                    return db.U_User.ToList();
                else
                    return db.U_User.Where(where).ToList();
            }
        }
    }

FindAll declare a method parameter is empty Lambda expressions ( Expression <Func <u_user, BOOL >> represents a Lambda Expression )

Wherein Func <U_User, bool> using generic delegate incoming U_User, returns a bool

Method Invocation

        public ActionResult Index()
        {
            //List<U_User> userList = userBLL.FindAll(c => c.UserName == "nee32");
            //List<U_User> userList = userBLL.FindAll(c => c.UserName == "nee32" && c.Status == 1);
            List<U_User> userList = userBLL.FindAll();
            return View();
        }

With Expression expression paging, attention  must be sorted before Linq paging  paging code is as follows

    /// <typeparam name = "TKey" > ordered field type </ typeParam> 
    /// <param name = "the pageIndex"> this page </ param> 
    /// <param name = "the pageSize"> per article number </ param> 
    /// <param name = "OrderBy"> Lambda expressions sort field </ param> 
    /// <param name = "WHERE"> Lambda query expression </ param> 
    /// <Returns > </ Returns> 
    public List <u_user> getPageList <TKey> (the pageIndex int, int the pageSize, the Expression <Func <u_user, TKey >> OrderBy, the Expression <Func <u_user, BOOL >> WHERE = null) 
    { 
        the using (DB EFContext EFContext new new = ()) 
        { 
            var = Query SELECT D from D in db.U_User; 
            IF (WHERE!= null)
            {
                query = query.Where(where);
            }
            var data = query.OrderBy(orderby)
            .Skip((pageIndex - 1) * pageSize)
            .Take(pageSize)
            .ToList();

            return data;
        }
    }

Paging call

    public ActionResult Index()
    {
        List<U_User> userList = userBLL.GetPageList(1, 20, c => c.CreateTime, c => c.UserName == "nee32");
        return View(userList);
    }







参考官网http://tutorials.jenkov.com/java/lambda-expressions.html

Guess you like

Origin www.cnblogs.com/xiaohuomiao/p/11040555.html