EF 里的 join and Group Join
join
var skip = pageSize * (pageIndx - 1); pageCount = _db.Orders.Count(); return _db.Orders.OrderByDescending(c=>c.ID).Skip(skip).Take(pageSize) .Join(_db.Income, a => a.ID, b => b.OrderId, (a, b) => new OrdersInComeModel { ID=a.ID, incometype=b.incometype, Amount=a.Amount, BuyDate=a.BuyDate, CancelDate=a.CancelDate, IncomeAmount=b.IncomeAmount, IncomeDate=b.IncomeDate, State=a.State }).ToList();
left join
var skip = pageSize * (pageIndx - 1); pageCount = _db.Orders.Count(); var lsw= _db.Orders.OrderByDescending(a => a.ID).Skip(skip).Take(pageSize); var rt=(from l in lsw join i in _db.Income on l.ID equals i.OrderId into os from f in os.DefaultIfEmpty() select new OrdersInComeModel { ID = l.ID, incometype = f.incometype, Amount = l.Amount, BuyDate = l.BuyDate, CancelDate = l.CancelDate, IncomeAmount = f.IncomeAmount, IncomeDate = f.IncomeDate, State = l.State }).ToList();
Group Join
一个KEY对象对应一个集合(返回子表对应集合)
var data = dbContext.Categories.GroupJoin(dbContext.Products, c => c.CategoryID, p => p.CategoryID, (c, p) => new { CategoryID = c.CategoryID, ProductList = p });