我定义了两个实体,它们分别映射到数据库中的两个表。在SQL中,我会这样进行连接:
select *
from tableA a
left outer join tableB b on b.ID = a.ID
where some condition
我该如何使用LINQ查询实现这一点?
我定义了两个实体,它们分别映射到数据库中的两个表。在SQL中,我会这样进行连接:
select *
from tableA a
left outer join tableB b on b.ID = a.ID
where some condition
使用Lambda表达式可以使用GroupJoin
示例:
var query =
People
.GroupJoin(
Pets,
person => person.PersonId,
pet => per.Owner,
(person, petCollection) =>
new
{
Person = person,
Pets = petCollection.Select(pet => pet.Name),
});
请参见:MSDN上的C#编程指南:如何执行左外连接。
例如:
var query = from person in people
join pet in pets on person equals pet.Owner into gj
from subpet in gj.DefaultIfEmpty()
select new { person.FirstName, PetName = (subpet == null ? String.Empty : subpet.Name) };