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

评价
