排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:
50010702506256
50010702506256
欢迎加群交流技术
分类:
EF
linq ( DefaultIfEmpty()方法是核心方法,添加此方法在后台才能产生 lift join的SQL语句,不添加则为join)
//使用linq
var database = from c in oaentities.Userchild
join f in oaentities.Userfather on c.Fid equals f.Cid into leftjointemp
from leftjoin in leftjointemp.DefaultIfEmpty()
select new
{
c.sid,
c.Username,
leftjoin.Fathername
};
var joinfdatabase = database.ToList();lamdba表达式(使用GroupJoin ,GroupJoin是一对多的方法,SelectMany是一对一的方法)
var database = oaentities.Userchild.GroupJoin(oaentities.Userfather, a => a.Fid, b => b.Cid, (user, parent) => new
{
//主表字段可以点出来
user = user,
//附表字段不能点出来
Father = parent
}).SelectMany(a => a.Father.DefaultIfEmpty(), (last, parent) => new LeftJoinDatabeseController {
//给需要显示的字段赋值(LeftJoinDatabeseController 类需要自己创建,需要显示的字段是这个类的成员)
sid = last.user.sid,
Username = last.user.Username,
Fathername = parent.Fathername
});
//向前台输出
ViewBag.joinfdatabase = database.ToList();评价