连接子句中的可选条件 - Linq
Optional condition in join clause - Linq
我有以下 linq 查询
var resultGuardian = from s in _db.Students
join sg in _db.StudentGuardians on s.StudentID equals sg.StudentID
join g in _db.Guardians on sg.GuardianId equals g.GuardianId
join lr in _db.luRelationTypes on sg.RelationTypeID equals lr.RelationTypeID
join ga in _db.GuardianAddresses on g.GuardianId equals ga.GuardianId
join ad in _db.Addresses on ga.AddressID equals ad.AddressID
join lt in _db.luAddressTypes on ad.AddressTypeID equals lt.AddressTypeID
join lg in _db.luGenders on g.GenderID equals (int?)lg.GenderID into ssts
from gdnr in ssts.DefaultIfEmpty()
where
s.TenantID == tenantid && sg.TenantID == tenantid && g.TenantID == tenantid &&
s.StatusId == (int?)Extension.StatusType.Active //1
&& g.StatusID == (int?)Extension.StatusType.Active &&
lr.StatusID == (int?)Extension.StatusType.Active && lr.TenantID == tenantid &&
s.StudentID == sid
select new
{
g.FirstName,
g.LastName,
IsPrimary = sg.IsPrimaryGuardian,
g.Education,
g.Email,
g.Phone,
lr.RelationCD,
ga.IsStudentAddress,
gdnr.GenderCD,
lt.AddressName,
ad.Address1,
ad.Address2,
ad.City,
ad.State,
ad.Zipcode
};
在上面的查询中,当 ad.AddressTypeID
为 null
时,它没有返回任何结果。
如果 ad.AddressTypeID
是 null
,我有要求,而不是从 LuAddressTypes
获取默认记录,其中 AddressTypeCd=1
。如果我尝试这种方式
join lt in LuAddressTypes on ad.AddressTypeID equals lt.AddressTypeID into v1
from v2 in v1.DefaultIfEmpty()
select new
{
v2.AddressName,
g.Phone,..
});
在结果 v2.AddressName
中总是返回 null
。我也无法指定 AddressTypeCd=1
where
条件。 AddressTypeCd=1
不是 ad.AddressTypeID
.
我需要 v2.AddressName
,其中 AddressTypeCd=1
。我怎样才能做到这一点?
查找相关实体 all related entities
您不能使用标准 LINQ join
,但在 LINQ to Entities 中,您可以使用基于相关 Where
的替代连接语法 - EF 足够智能,可以将其转换为 JOIN
.
在你的情况下,而不是
join lt in _db.luAddressTypes on ad.AddressTypeID equals lt.AddressTypeID
你可以使用
from lt in _db.luAddressTypes.Where(lt => ad.AddressTypeID == lt.AddressTypeID
|| (ad.AddressTypeID == null && lt.AddressTypeCd == 1))
翻译成这样
INNER JOIN [dbo].[LuAddressTypes] AS [Extent3]
ON ([Extent2].[AddressTypeID] = [Extent3].[AddressTypeID])
OR (([Extent2].[AddressTypeID] IS NULL) AND (1 = [Extent3].[AddressTypeCd]))
如果你想要LEFT OUTER JOIN
,只需在上面一行的末尾添加.DefaultIfEmpty()
。
我有以下 linq 查询
var resultGuardian = from s in _db.Students
join sg in _db.StudentGuardians on s.StudentID equals sg.StudentID
join g in _db.Guardians on sg.GuardianId equals g.GuardianId
join lr in _db.luRelationTypes on sg.RelationTypeID equals lr.RelationTypeID
join ga in _db.GuardianAddresses on g.GuardianId equals ga.GuardianId
join ad in _db.Addresses on ga.AddressID equals ad.AddressID
join lt in _db.luAddressTypes on ad.AddressTypeID equals lt.AddressTypeID
join lg in _db.luGenders on g.GenderID equals (int?)lg.GenderID into ssts
from gdnr in ssts.DefaultIfEmpty()
where
s.TenantID == tenantid && sg.TenantID == tenantid && g.TenantID == tenantid &&
s.StatusId == (int?)Extension.StatusType.Active //1
&& g.StatusID == (int?)Extension.StatusType.Active &&
lr.StatusID == (int?)Extension.StatusType.Active && lr.TenantID == tenantid &&
s.StudentID == sid
select new
{
g.FirstName,
g.LastName,
IsPrimary = sg.IsPrimaryGuardian,
g.Education,
g.Email,
g.Phone,
lr.RelationCD,
ga.IsStudentAddress,
gdnr.GenderCD,
lt.AddressName,
ad.Address1,
ad.Address2,
ad.City,
ad.State,
ad.Zipcode
};
在上面的查询中,当 ad.AddressTypeID
为 null
时,它没有返回任何结果。
如果 ad.AddressTypeID
是 null
,我有要求,而不是从 LuAddressTypes
获取默认记录,其中 AddressTypeCd=1
。如果我尝试这种方式
join lt in LuAddressTypes on ad.AddressTypeID equals lt.AddressTypeID into v1
from v2 in v1.DefaultIfEmpty()
select new
{
v2.AddressName,
g.Phone,..
});
在结果 v2.AddressName
中总是返回 null
。我也无法指定 AddressTypeCd=1
where
条件。 AddressTypeCd=1
不是 ad.AddressTypeID
.
我需要 v2.AddressName
,其中 AddressTypeCd=1
。我怎样才能做到这一点?
查找相关实体 all related entities
您不能使用标准 LINQ join
,但在 LINQ to Entities 中,您可以使用基于相关 Where
的替代连接语法 - EF 足够智能,可以将其转换为 JOIN
.
在你的情况下,而不是
join lt in _db.luAddressTypes on ad.AddressTypeID equals lt.AddressTypeID
你可以使用
from lt in _db.luAddressTypes.Where(lt => ad.AddressTypeID == lt.AddressTypeID
|| (ad.AddressTypeID == null && lt.AddressTypeCd == 1))
翻译成这样
INNER JOIN [dbo].[LuAddressTypes] AS [Extent3]
ON ([Extent2].[AddressTypeID] = [Extent3].[AddressTypeID])
OR (([Extent2].[AddressTypeID] IS NULL) AND (1 = [Extent3].[AddressTypeCd]))
如果你想要LEFT OUTER JOIN
,只需在上面一行的末尾添加.DefaultIfEmpty()
。