[英]Optional condition in join clause - Linq
I have below linq query 我有下面的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
};
In above query when ad.AddressTypeID
is null
, it is not returning any result. 在上面的查询中,当ad.AddressTypeID
为null
,它不返回任何结果。
I have requirement if ad.AddressTypeID
is null
,than from LuAddressTypes
fetch default record where AddressTypeCd=1
. 我需要ad.AddressTypeID
为null
,而不是从LuAddressTypes
获取其中AddressTypeCd=1
默认记录。 If I try this way 如果我这样尝试
join lt in LuAddressTypes on ad.AddressTypeID equals lt.AddressTypeID into v1
from v2 in v1.DefaultIfEmpty()
select new
{
v2.AddressName,
g.Phone,..
});
in result v2.AddressName
always returning null
. 结果v2.AddressName
始终返回null
。 I am unable to specify AddressTypeCd=1
where
condition as well. 我不能指定AddressTypeCd=1
where
状态为好。 AddressTypeCd=1
is not ad.AddressTypeID
. AddressTypeCd=1
不是ad.AddressTypeID
。
I need v2.AddressName
where AddressTypeCd=1
. 我需要v2.AddressName
,其中AddressTypeCd=1
。 How can I do that? 我怎样才能做到这一点? Find related entities all related entities 查找相关实体所有相关实体
You can't use the standard LINQ join
, but in LINQ to Entities you could use the alternative join syntax based on correlated Where
- EF is smart enough to translate it to JOIN
. 您不能使用标准的LINQ join
,但是在LINQ to Entities中,您可以使用基于相关的替代连接语法Where
-EF足够智能,可以将其转换为JOIN
。
In your case, instead of 在您的情况下,
join lt in _db.luAddressTypes on ad.AddressTypeID equals lt.AddressTypeID
you could use 你可以用
from lt in _db.luAddressTypes.Where(lt => ad.AddressTypeID == lt.AddressTypeID
|| (ad.AddressTypeID == null && lt.AddressTypeCd == 1))
which is translated to something like this 这被翻译成这样的东西
INNER JOIN [dbo].[LuAddressTypes] AS [Extent3]
ON ([Extent2].[AddressTypeID] = [Extent3].[AddressTypeID])
OR (([Extent2].[AddressTypeID] IS NULL) AND (1 = [Extent3].[AddressTypeCd]))
If you want LEFT OUTER JOIN
, simply add .DefaultIfEmpty()
at the end of the above line. 如果您想要LEFT OUTER JOIN
,只需在上一行的末尾添加.DefaultIfEmpty()
即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.