In C# Linq to Objects, it is very simple to use the "Join-On-Equals-Into" clause to execute the left external connection. However, in some cases, using the where clause may be more desirable.
For internal connection, there is a simple solution:
However, for the left external connection, the problem is that the number of operational number of the right side is not found when the matching item cannot be found. Consider the following attempts:
<code class="language-csharp">List<joinpair> innerFinal = (from l in lefts from r in rights where l.Key == r.Key select new JoinPair { LeftId = l.Id, RightId = r.Id});</code>
This method has a disadvantage: it assurates the silent recognition of the left side button that is not matched, which may cause ambiguity. To overcome this problem, we can use the "DefaultIFEMPTY" method:
<code class="language-csharp">List<joinpair> leftFinal = (from l in lefts from r in rights select new JoinPair { LeftId = l.Id, RightId = ((l.Key==r.Key) ? r.Id : 0 });</code>
This solution uses the "DEFAULTIFEMPTY" method to provide the default value to the non -matching left button to ensure the accuracy of the results.
<code class="language-csharp">var q = from l in lefts join r in rights on l.Key equals r.Key into ps_jointable from p in ps_jointable.DefaultIfEmpty() select new JoinPair { LeftId = l.Id, RightId = p == null ? 0 : p.Id };</code>
The above is the detailed content of How to Perform a Left Outer Join in LINQ Without Using `join-on-equals-into` Clauses?. For more information, please follow other related articles on the PHP Chinese website!