tnblog
首页
视频
资源
登录

.NET MVC 中lamdba实现left join

5384人阅读 2019/10/17 17:50 总访问:94055 评论:0 收藏:0 手机
分类: .NET MVC

1、left join 用GroupJoin来实现,join的返回结果为一对一,GroupJoin返回结果为一对多,相当于集合中还有集合

        public ActionResult Index()
        {
            //left join (lamdba写法)
            var result = oae.userinfo.GroupJoin(oae.noteinfo, a => a.userId, b => b.userId, (u, n) => new
            {
                userName = u.userName,
                notes = n
            }).SelectMany(a => a.notes.DefaultIfEmpty(), (a, b) => new
            {               userName = a.userName,
                noteRemark = b.noteRemark
            }).ToList();
            
            List<users> list = new List<users>();
            foreach (var item in result)
            {
                users user = new users();
                user.userId = item.userId;
                user.userName = item.userName;
                user.noteRemark = item.noteRemark;
                list.Add(user);
            }
            ViewBag.count = list.Count;
            return View(list);
            
        }

a为上一次返回结果,b为前一个表,SelectMany方法返回一个实体集合,再把返回结果解析到一个具体的实体集合,再返回给前台,在前台就可以显示数据了。(自我理解)


评价
网络上你重拳出击,现实中你刚上初一
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
欢迎加群交流技术