EF连表查询

EF连表查询

二张表Join

b代表主表Business,p代表副标

  public ActionResult list()
        {
            //如果想N张表,再向db.BusinessPress后面加
            var data = db.Business.Join(db.BusinessPress, b => b.BPress, p => p.ID, (b, p) => new
            {
                ID = b.ID,
                BName = b.BName,
                BPress = p.PName,
                Price = b.Price,    
            }).ToList();

            //ViewModel
            List<VMBusiness> list = new List<VMBusiness>();
            data.ForEach(item =>
            {
                list.Add(new VMBusiness()
                {
                    ID = item.ID,
                    BName = item.BName,
                    BPress = item.BPress,
                    Price = item.Price,
             
                });
            });
            return View(list);
        }

 三张表Join

b代表主表Business,p代表副标Businesspress,a代表b,p合成的表,c代表LoginPress表

            var data = db.Business.Join(db.BusinessPress, b => b.BPress, p => p.ID, (b, p) => new
            {
                b,
                p
            }).Join(db.LoginPress, a => a.b.LoginID, c => c.LoginID, (a, c) => new
            {
                ID = a.b.ID,
                BName = a.b.BName,
                BPress = a.p.PName,
                Price = a.b.Price,
                Quantity = a.b.Quantity,
                Photo = a.b.Photo,
                LoginID = c.LoginName
            }).ToList();

N表Join

就是在后面+N个join

注意类型

posted @ 2020-08-08 20:50  我的未来方程式i  阅读(217)  评论(0编辑  收藏  举报