单条件:
var query = from person in people
join pet in pets on person equals pet.Owner
select new { OwnerName = person.FirstName, PetName = pet.Name };
多条件:
IEnumerable<string> query = from employee in employees
join student in students
on new { employee.FirstName, employee.LastName }
equals new { student.FirstName, student.LastName }
select employee.FirstName + " " + employee.LastName;
左连接:
var list = from a in db.tab1
join b in db.tab2 on a.Id equals b.Id into bb
from bbdata in bb.DefaultIfEmpty()
select new
{
info1=a,
info2 = bbdata ,
};
db.tab2就是右表了,等于:select * from tab1 as a left join tab2 as b on a.Id=b.Id
db.Products
.GroupJoin(db.Categories,
(Product p) => p.CategoryId,
(Category c) => c.CategoryId,
(prod, cs) => new { prod, cs }) // cs is IEnumerable<Category>
.SelectMany(prodCats => prodCats.cs.DefaultIfEmpty(), (prodCats, c) =>
new
{
prodCats.prod
categoryName = c.CategoryName
});
https://www.cnblogs.com/ybtools/p/6559292.html
https://www.cnblogs.com/zitjubiz/p/a-left-outer-join-using-linq-extension-methods.html
浙公网安备 33010602011771号