首页 > 其他分享 >EF操作_多表联查lambda和linq

EF操作_多表联查lambda和linq

时间:2022-10-19 14:36:41浏览次数:54  
标签:UserName UserParent 多表 EF linq fHZMEntities var new Id

单表

sql

List<UserInfo> userList=fHZMEntities.Database.SqlQuery<UserInfo>("select * from UserInfo").ToList();

Linq

List<UserInfo> userList = (from a in fHZMEntities.UserInfo
where (string.IsNullOrEmpty(userinfo.UserName) || a.UserName == userinfo.UserName)
select a).ToList();

Lambda

List<UserInfo> userList = fHZMEntities.UserInfo.Where(a => (string.IsNullOrEmpty(userinfo.UserName) || a.UserName == userinfo.UserName)
&& (string.IsNullOrEmpty(userinfo.Number) || a.Number == userinfo.Number)
).ToList();

两表join

sql

List<UserParentViewModel> UserParentViewModelList = fHZMEntities.Database.SqlQuery<UserParentViewModel>("select *from UserInfo join UserParent on UserInfo.UserParent =UserParent.id").ToList();

Linq

var jointemp = from a in fHZMEntities.UserInfo
              join b in fHZMEntities.UserParent on a.UserParent equals b.id
              select new
               {
                  a.Id,
                   a.UserName,
                 b.FatherName
             };

Lambda

第一个参数:你要join的表
第二,三个参数:join连接条件
第四个参数:返回的结果

var UserParentViewModelList = fHZMEntities.UserInfo.Join(fHZMEntities.UserParent, a => a.UserParent, b => b.id, (a, b) => new
             {
                 a.Id,
                 a.UserName,
                 b.FatherName
             }).ToList();

两表leftjoin

Linq

var jointemp = from a in fHZMEntities.UserInfo
    join p in fHZMEntities.UserParent on a.UserParentId equals p.id into leftjointemp
    from leftjon in leftjointemp.DefaultIfEmpty()
    select new
{
    a.Id,
    a.UserName,
    leftjon.FatherName
};
var result = jointemp.ToList();

Lambda

//GroupJoin一对多
            var jointemp = fHZMEntities.UserInfo.GroupJoin(fHZMEntities.UserParent, a => a.UserParentId, b => b.id, (user, parent) => new
            {
                user = user,
                Father = parent
            }).SelectMany(a => a.Father.DefaultIfEmpty(), (last, parent) => new UserParentViewModel
            {
                Id = last.user.Id,
                UserName = last.user.UserName,
                FatherName = parent.FatherName
            });
            //SelectMany一对一

三表join

Linq

var jointemp = from u in fHZMEntities.UserInfo
               join p in fHZMEntities.UserParent on u.UserParentId equals p.id
              join s in fHZMEntities.Score on u.Id equals s.UserId
               select new
              {
                  u.Id,
                  u.UserName,
                  p.FatherName,
                  s.sub,
                  s.userScore
               };
var result = jointemp.ToList();

Lambda

      var jointemp = fHZMEntities1.UserInfo.Join(fHZMEntities1.UserParent, a => a.UserParentId, b => b.id, (a, b) => new
            {
                a.Id,
                a.UserName,
                a.Number,
                a.UClass,
                b.FatherName
            }).Join(fHZMEntities1.Score, a => a.Id, b => b.UserId, (a, b) => new
            {
                a.Id,
                a.UserName,
                a.Number,
                a.UClass,
                b.sub,
                b.userScore,
                a.FatherName
            });
            var result = jointemp.ToList();

三表leftjoin

Linq

var list = from u in fHZMEntities1.UserInfo
    join p in fHZMEntities1.UserParent on u.UserParentId equals p.id into leftjointemp
    from leftjointemp2 in leftjointemp.DefaultIfEmpty()
    join s in fHZMEntities1.Score on u.Id equals s.UserId into scoretemp
    from leftscore in scoretemp.DefaultIfEmpty()
    select new
{
    u.Id,
    u.UserName,
    u.Number,
    u.UClass,
    leftscore.sub,
    leftscore.userScore,
    leftjointemp2.FatherName
};
var result = list.ToList();

Lambda

var result = fHZMEntities1.UserInfo.GroupJoin(fHZMEntities1.UserParent, a => a.UserParentId, b => b.id, (a, b) => new
            {
                a.Id,
                a.UserName,
                a.Number,
                a.UClass,
                UserParent = b
            }).GroupJoin(fHZMEntities1.Score, a => a.Id, b => b.UserId, (a, b) => new
            {
                a.Id,
                a.UserName,
                a.Number,
                a.UClass,
                a.UserParent,
                Score = b
            }).SelectMany(a => a.UserParent.DefaultIfEmpty(), (m, n) => new
            {
                m.Id,
                m.UserName,
                m.Number,
                m.UClass,
                m.Score,
                n.FatherName
            }).SelectMany(a => a.Score.DefaultIfEmpty(), (m, n) => new
            {
                m.Id,
                m.UserName,
                m.Number,
                m.UClass,
                n.sub,
                n.userScore,
                m.FatherName,
            });
            var list = result.ToList();

join的连接之后的参数的理解

leftjoin参数理解

标签:UserName,UserParent,多表,EF,linq,fHZMEntities,var,new,Id
From: https://www.cnblogs.com/dongh/p/16806079.html

相关文章

  • K8S statefulset
       StatefulSet详解kubectlexplainsts.spec:主要字段解释replicas:副本数selector:那个pod是由自己管理的serviceName:必须关联到一个无头服务商template:定义pod模......
  • Educational Codeforces Round 137 (Rated for Div. 2) - E. FTL
    DPProblem-E-Codeforces题意有个BOSS有\(H\;(1<=H<=5000)\)血量,\(s\)点防御有两种武器可用攻击BOSS,伤害分别为\(p_1,p_2\;(s<min(p_1,p_2)<=5000)\),冷却......
  • 论文解读(GGD)《Rethinking and Scaling Up Graph Contrastive Learning: An Extremely
    论文信息论文标题:RethinkingandScalingUpGraphContrastiveLearning:AnExtremelyEfficientApproachwithGroupDiscrimination论文作者:YizhenZheng,ShiruiP......
  • Codeforces Round #712 A
    A.BalancetheBits显然对于一个字符串s我们每一对0之间必须是()一个合法的括号才行)(也可以显然是等价的因为你a拿前者b就会拿后者所以这就要求了我们0的个数必须是偶......
  • .NET6 EF CORE实现全局过滤查询
    1、设置一个基类(BaseEntity),包含IsDeleted属性publicclassBaseEntity{[Key,DatabaseGenerated(DatabaseGeneratedOption.None)]publiclongI......
  • ‘project JDK is not define‘ 解决方案 打开【File - Project Structure】 修改【M
    ‘projectJDKisnotdefine‘解决方案最近想玩玩javap反编译然后捣鼓来捣鼓去,出现了这个问题,解决方案之一1.打开【File-ProjectStructure】2.修改【Module......
  • MySQL 错误码: 1067Invalid default value for ‘xxx‘问题解决
    声明,此文为转载内容,原作者地址为:https://blog.csdn.net/qq_38974638/article/details/1223005381.问题描述:错误码:1067Invaliddefaultvaluefor'gmt_create......
  • Educational Codeforces Round 107 D
    D.MinCostString显然我们对于每两个组都要本质不同我们考虑本质不同两个组的数量为k^2我们考虑如何构造将这k^2的连接起来不然显然如果一个借着一个显然会产生新的......
  • 游戏效果(GameplayEffect)
    GE的主要用途是通过改变目标或自身的Attribute或者Tags实现诸如造成伤害、治疗、强化、削弱等效果,GE也提供了Execution来执行逻辑,提供了相当大的灵活性。(我个人......
  • Codeforces Round #713 E
    E.PermutationbySum显然轻松上下界判断考虑如何构造我们将它整成最小的样子1234....k个从后往前要是他需要我们就直接加上去就可以了#include<bits/stdc++.h......