Lambda表达式应用

扶醉桌前 提交于 2020-02-11 09:32:36

Lambda表达式实际上是一个匿名函数,它可以说是对LINQ的补充。

由于LINQ查询关键字和IEnumerable接口的方法之间有一个对应关系,但是LINQ查询表达式中可以使用的查询功能很少。

在实际开发中通过查询结果或数据源进行方法调用,从而进行更多的查询操作。

由于Lambda表达式是匿名函数,它可以赋值到一个委托,而在IEnumerable接口的方法中很多通过函数委托来实现自定义运算、条件等操作,所以Lambda表达式在LINQ中被广泛使用。

 详情请查阅:https://www.php.cn/csharp-article-353245.html

根据条件查询

        /// <summary>
        /// 根据条件查询
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public List<User> GetUser(string UserID)
        {
            using (FamilyServerEntities Family = new FamilyServerEntities())
            {
                List<User> users = Family.User.Where(a => a.UserID == UserID).ToList();
                return users;
            }
        }

根据条件修改

        /// <summary>
        /// 根据条件修改
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="UserName"></param>
        /// <returns></returns>
        public User Update(string UserID, string UserName)
        {
            using (FamilyServerEntities Family = new FamilyServerEntities())
            {
                //根据条件查询出来
                User user = Family.User.Where(a => a.UserID == UserID).FirstOrDefault();
                if (user != null)
                {
                    //修改指定数据
                    user.UserName = UserName;
                    //保存
                    Family.SaveChanges();
                }
                return user;
            }
        }

增加数据

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public User UserADD(User user)
        {
            using (FamilyServerEntities Family = new FamilyServerEntities())
            {
                Family.User.Add(
                    new User()
                    {
                        UserID = user.UserID,
                        UserName = user.UserName,
                        UserSet = user.UserSet
                    });
                if (Family.SaveChanges() > 0)
                {
                    return user;
                }
                else
                    return null;
            }
        }

增加多条

        /// <summary>
        /// 添加多条
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public List<User> UserADDList(List<User> user)
        {
            using (FamilyServerEntities Family = new FamilyServerEntities())
            {
                for (int i = 0; i < user.Count; i++)
                {
                    Family.User.Add(
                      new User()
                      {
                          UserID = user[i].UserID,
                          UserName = user[i].UserName,
                          UserSet = user[i].UserSet
                      });
                }

                if (Family.SaveChanges() > 0)
                {
                    return user;
                }
                else
                    return null;
            }
        }

根据条件删除数据

  /// <summary>
        /// 根据条件删除数据
        /// </summary>
        /// <param name="UserID"></param>
        public void Deleted(string UserID)
        {
            using (FamilyServerEntities Family = new FamilyServerEntities())
            {
                //根据条件查询出一条数据到user 
                User user = Family.User.Where(a => a.UserID == UserID).FirstOrDefault();
                if (user != null)
                {
                    //如果有数据那么就删除 并且保存
                    Family.User.Remove(user);
                }
                if (Family.SaveChanges() > 0)
                {
                    
                }
            }
        }

查询多表

    /// <summary>
        /// 查询多表
        /// </summary>
        /// <returns></returns>
        public static  User Getuser()
        {
            using (FamilyServerEntities Family = new FamilyServerEntities())
            {
                User user = new User();
                var result = Family.User.GroupJoin(Family.Work, u => u.UserworkID, w => w.UserworkID, (u, w) => new
                {
                    Work = w,
                    u.UserID,
                    u.UserName,
                    u.UserSet,
                    u.Userphone,
                    u.UserCreationtime,
                    u.UserlevelID,
                    u.UserTypeID,
                    u.UserhobbyID
                }).SelectMany(u => u.Work, (u, w) => new
                {
                    u.UserID,
                    u.UserName,
                    u.UserSet,
                    u.Userphone,
                    u.UserCreationtime,
                    u.UserlevelID,
                    u.UserTypeID,
                    u.UserhobbyID,
                    w.WorkDetailsID,
                    w.UserworkType
                }).GroupJoin(Family.level, u => u.UserlevelID, l => l.UserlevelID, (u, l) => new
                {
                    level = l,
                    u.UserID,
                    u.UserName,
                    u.UserSet,
                    u.Userphone,
                    u.UserCreationtime,
                    u.UserworkType,
                    u.UserTypeID,
                    u.UserhobbyID,
                    u.WorkDetailsID,
                }).SelectMany(u => u.level, (u, l) => new
                {
                    u.UserID,
                    u.UserName,
                    u.UserSet,
                    u.Userphone,
                    u.UserCreationtime,
                    u.UserTypeID,
                    u.UserhobbyID,
                    u.WorkDetailsID,
                    u.UserworkType,
                    l.UserleverlType
                }).GroupJoin(Family.UserType, u => u.UserTypeID, t => t.UserTypeID, (u, t) => new
                {
                    UserType = t,
                    u.UserID,
                    u.UserName,
                    u.UserSet,
                    u.Userphone,
                    u.UserCreationtime,
                    u.UserTypeID,
                    u.UserhobbyID,
                    u.WorkDetailsID,
                    u.UserworkType,
                    u.UserleverlType
                }).SelectMany(u => u.UserType, (u, t) => new
                {
                    u.UserID,
                    u.UserName,
                    u.UserSet,
                    u.Userphone,
                    u.UserCreationtime,
                    t.UserType1,
                    u.UserhobbyID,
                    u.WorkDetailsID,
                    u.UserworkType,
                    u.UserleverlType
                }).GroupJoin(Family.WorkDetails, u => u.WorkDetailsID, d => d.WorkDetailsID, (u, d) => new
                {
                    WorkDetails = d,
                    u.UserID,
                    u.UserName,
                    u.UserSet,
                    u.Userphone,
                    u.UserCreationtime,
                    u.UserType1,
                    u.UserhobbyID,
                    u.WorkDetailsID,
                    u.UserworkType,
                    u.UserleverlType
                }).SelectMany(u => u.WorkDetails, (u, d) => new
                {
                    u.UserID,
                    u.UserName,
                    u.UserSet,
                    u.Userphone,
                    u.UserCreationtime,
                    u.UserType1,
                    u.UserhobbyID,
                    d.WorkDetailsSituation,
                    u.UserworkType,
                    u.UserleverlType
                }).GroupJoin(Family.Userhobby, u => u.UserhobbyID, h => h.UserhobbyID, (u, h) => new
                {
                    Userhobby = h,
                    u.UserID,
                    u.UserName,
                    u.UserSet,
                    u.Userphone,
                    u.UserCreationtime,
                    u.UserType1,
                    u.UserhobbyID,
                    u.WorkDetailsSituation,
                    u.UserworkType,
                    u.UserleverlType
                }).SelectMany(u => u.Userhobby, (u, h) => new
                {
                    u.UserID,
                    u.UserName,
                    u.UserSet,
                    u.Userphone,
                    u.UserCreationtime,
                    u.UserType1,
                    u.WorkDetailsSituation,
                    u.UserworkType,
                    u.UserleverlType,
                    h.Userhobby1
                }).Select(u => new
                {
                    UserID = u.UserID,
                    UserName = u.UserName,
                    UserSet = u.UserSet,
                    Userphone = u.Userphone,
                    UserCreationtime = u.UserCreationtime,
                    UserType1 = u.UserType1,
                    WorkDetailsSituation = u.WorkDetailsSituation,
                    UserworkType = u.UserworkType,
                    UserleverlType = u.UserleverlType,
                    Userhobby1 = u.Userhobby1
                }).ToList();
                if (result.Count > 0)
                {
                    for (int i = 0; i < result.Count; i++)
                    {
                        foreach (var item in result)
                        {
                            user.UserID = item.UserID;
                            user.UserName = item.UserName;
                            user.UserSet = item.UserSet;
                            user.Userphone = item.Userphone;
                        }
                    }
                }
                
                return user;
            }
        }

数据库

总结一下:其实挺简单的

好了废话不多少了,我滚了。

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!