我有这个 SQL 查询
select at.description,
a.Address1,
a.Address2,
a.City
from address_types at
left join Address a
on a.addresstype = at.addresstype
and a.addressid=24
想用 Linq Lambda 表达式写,有什么线索吗?
谢谢!
我有这个 SQL 查询
select at.description,
a.Address1,
a.Address2,
a.City
from address_types at
left join Address a
on a.addresstype = at.addresstype
and a.addressid=24
想用 Linq Lambda 表达式写,有什么线索吗?
谢谢!
我宁愿以下列方式简化它
(from at in address_types
join a in address
on at.addresstype equals a.addresstype into tt
from ar in tt.DefaultIfEmpty()
where (ar != null && ar.addressid == 24)
select new
{
at.Description,
ar.Address1,
ar.Address2,
ar.City
})
我们不需要这个检查“(ja == null)”的原因是因为我们已经在“where (ar != null && ar.addressid == 24)”这一行中限制了我们的集合。
同时这个查询有效,我的观点是 LEFT JOIN 在 SQL 查询中也是多余的。由于您的条件“a.addressid = 24”,您不会得到 NULL 行,因此 SQL 也可以重写为 INNER JOIN 和 LINQ 查询
(from at in address_types
join a in address
on at.addresstype equals a.addresstype
where at.addressid == 24
select new
{
at.Description,
a.Address1,
a.Address2,
a.City
})
你可以尝试这样的事情:
var resultset = (from at in addresstypes
join a in addresses
on at.AddressType equals a.AddressType into joinaddress
from ja in joinaddress.DefaultIfEmpty()
where (ja != null && ja.AddressID == 24)
select new
{
AddressType = at.AddressType,
AddressID = ja == null ? 0 : ja.AddressID,
Address1 = ja== null ? string.Empty : ja.Address1
});