Lambda表达式应用

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

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

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

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

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

根据条件查询

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

根据条件修改

        /// 
        /// 根据条件修改
        /// 
        /// 
        /// 
        /// 
        public static int 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 1;
                }
                return 0;
            }
        }

修改多条

        /// 
        /// 修改多条
        /// 
        /// 
        /// 
        public static int Edits(List list)
        {
            using (FamilyServerEntities Family = new FamilyServerEntities())
            {
                User user = Family.User.FirstOrDefault();
                if (user != null)
                {
                    foreach (var item in list)
                    {
                        user.UserName = item.UserName;
                        Family.SaveChanges();
                    }
                    return 1;
                }
                return 0;
            }
        }

增加数据

        /// 
        /// 添加数据
        /// 
        /// 
        /// 
        public static int 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 1;
                }
                else
                    return 0;
            }
        }

 

增加多条

        /// 
        /// 添加多条
        /// 
        /// 
        /// 
        public List UserADDList(List user)
        {
            using (FamilyServerEntities Family = new FamilyServerEntities())
            {
                if (user.Count > 0)
                {
                    foreach (var item in user)
                    {
                        Family.User.Add(new User()
                        {
                            UserID = item.UserID,
                            UserName = item.UserName,
                            UserSet = item.UserSet
                        });
                    }
                    if (Family.SaveChanges() > 0)
                    {
                        return user;
                    }
                    else
                        return null;
                }
                return null;
            }
        }

根据条件删除数据

  /// 
        /// 根据条件删除数据
        /// 
        /// 
        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)
                {
                    
                }
            }
        }

删除多条数据

        /// 
        /// 根据条件删除多条数据
        /// 
        /// 
        public int Deleted(string UserID)
        {
            using (FamilyServerEntities Family = new FamilyServerEntities())
            {
                //根据条件查询数据到user 
                List user = Family.User.Where(a => a.UserID == UserID).ToList();
                if (user != null)
                {
                    foreach (var item in user)
                    {                       
                        Family.User.Remove(item);
                    }
                }
                return Family.SaveChanges();
            }
        }

 

查询多表

    /// 
        /// 查询多表
        /// 
        /// 
        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;
            }
        }

数据库

Lambda表达式应用_第1张图片

总结一下:其实挺简单的

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

 

你可能感兴趣的:(Lambda表达式应用)