Sql 使用LINQ使用两个表中的替代地址生成一个表
我试图从两个sql表构建一个数据表。我想使用一个辅助表将第二个表合并到第一个表中?我当前的代码跳过没有其他地址的记录 目标是返回一个如下所示的表:Sql 使用LINQ使用两个表中的替代地址生成一个表,sql,vb.net,linq,Sql,Vb.net,Linq,我试图从两个sql表构建一个数据表。我想使用一个辅助表将第二个表合并到第一个表中?我当前的代码跳过没有其他地址的记录 目标是返回一个如下所示的表: Dim db As New DataClassesDataContext Dim buildingTable = From b In db.Buildings Join s In db.States On b.StateId Equals s.StateId Join a In d
Dim db As New DataClassesDataContext
Dim buildingTable = From b In db.Buildings
Join s In db.States
On b.StateId Equals s.StateId
Join a In db.Building_Alternate_Addresses
On b.BuildingId Equals a.BuildingId
Where b.Active = True
Order By b.Address1
已经有一段时间没有写VB.NET了,但是除非我严重误解了什么,否则您正在尝试在
构建
和构建备用地址
之间进行左连接,在这种情况下,您应该可以这样做,提供的楼宇备用\u地址
仅包含地址2的非空值行
:
from b in Building
from baa in Building_Alternate_Address
.Where(Function(x as Building)
return x.BuildingId == b.BuildingId
End Function)
.DefaultIfEmpty()
where b.Active
orderby b.Address1
select New With {
.BuildingId = b.BuildingId,
.Address1 = b.Address1,
.Address2 = baa.Address2,
.City = b.City,
.StateId = b.StateId,
.ZipCode = b.ZipCode,
.NickName = b.NickName,
.Active = b.Active
}
Where
应始终位于OrderBy
之前您是否尝试在state表之前加入备用地址表?我没有。现在让我试试。仍然只给我一个有替代地址的建筑列表。尝试一些类似“从TXs中的lst1将lst2加入lst1中的TYs中。ID等于lst2.ID从yG中的y1转换为yG。DefaultIfEmpty()选择新的{X=lst1,Y=y1}”@Minel:如果解决方案对你有效,别忘了奖励;]我在说什么?谈论假设结果!让我们回到我原来的答案,好吗?