Linq join that accepts null values
本问题已经有最佳答案,请猛点这里访问。
我有一个这样的LINQ查询:
1 2 3 4 5 6 7 8 9 10 11 12 | 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, }); |
这个查询工作正常。但我想有一些记录他们中的一些人没有同伴。我该怎么做?此查询返回只具有对等方的记录,我如何才能拥有既具有对等方又不具有对等方的记录。如果该记录中存在对等方,我想显示对等方的名称。谢谢。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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, }); |
号
它被称为左外联接。尝试:
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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, }); |