我有这样的Linq查询:
var items = from v in work.GetRepo<VW_V>().Query
join k in work.GetRepo<K>().Query on v.Loc_Id equals k.Id
join p in work.GetRepo<P>().Query on v.Peer_Id equals p.Id
join tt in work.GetRepo<TT>().Query on v.Item_Id equals tt.Id
select (new MyModel
{
Id = v.Id,
Location = k != null ? k.Name : string.Empty,
ItemName = tt.Name,
Peer = p != null ? p.Name : string.Empty,
});
此查询正常。但我希望有记录表明他们中的一些人没有同伴。我怎样才能做到这一点?此查询返回仅具有Peers的记录,如何拥有具有peer且不具有peer的记录。如果该记录中存在peer,我想显示Peer的名字。感谢。
答案 0 :(得分:1)
它被称为左outer join。尝试:
var items = from v in work.GetRepo<VW_V>().Query
join k in work.GetRepo<K>().Query on v.Loc_Id equals k.Id
join p in work.GetRepo<P>().Query on v.Peer_Id equals p.Id into subpeer_j
from subpeer in subpeer_j.DefaultIfEmpty()
join tt in work.GetRepo<TT>().Query on v.Item_Id equals tt.Id
select (new MyModel
{
Id = v.Id,
Location = k != null ? k.Name : string.Empty,
ItemName = tt.Name,
Peer = subpeer != null ? subpeer.Name : string.Empty,
});
答案 1 :(得分:1)
var items = from v in work.GetRepo<VW_V>().Query
join k in work.GetRepo<K>().Query
on v.Loc_Id equals k.Id
join p in work.GetRepo<P>().Query
on v.Peer_Id equals p.Id
into pJoinData
from pJoinRecord in pJoinData.DefaultIfEmpty( )
join tt in work.GetRepo<TT>().Query
on v.Item_Id equals tt.Id
select (new MyModel
{
Id = v.Id,
Location = k != null ? k.Name : string.Empty,
ItemName = tt.Name,
Peer = pJoinRecord != null ? pJoinRecord.Name : string.Empty,
});