家里总停电

.NET MVC 中lamdba实现left join

电脑版发表于:2019/10/17 17:50

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方法返回一个实体集合,再把返回结果解析到一个具体的实体集合,再返回给前台,在前台就可以显示数据了。(自我理解)


关于TNBLOG
TNBLOG,技术分享。技术交流:群号677373950
ICP备案 :渝ICP备18016597号-1
App store Android
精彩评论
{{item.replyName}}
{{item.content}}
{{item.time}}
{{subpj.replyName}}
@{{subpj.beReplyName}}{{subpj.content}}
{{subpj.time}}
猜你喜欢