Home >Backend Development >C++ >How to Perform Left Outer Joins in LINQ Using Extension Methods?
Left outer connection is a connection operation. It returns all the lines in the left table (called the "Father" table), and the right table (called the "Zi" table) only matched row. If you can't find a matching line in the sub -table, the return value of the line in the parent table is null.
In linq, a method of executing the left and outer connection is to use the
method, which creates an anonymous type sequence that contains the attributes in the parent table and the sub -table. However, the more convenient way to achieve the same results is to use the and Join
expansion methods. GroupJoin
SelectMany
The left -outer connection syntax using the expansion method is as follows:
In this grammar:
<code class="language-csharp">var query = parentTable.GroupJoin( childTable, parentKeySelector, childKeySelector, (parent, child) => new { Parent = parent, Child = child.DefaultIfEmpty() }) .SelectMany( x => x.Child, (x, y) => new { Parent = x.Parent, Child = y });</code>
and
parentTable
and childTable
are Lambda expressions specified to connect the keys for two tables. parentKeySelector
childKeySelector
Methods to ensure that the NULL value is returned to the NULL value for a child who does not match the parent. GroupJoin
DefaultIfEmpty()
SelectMany
This query will return an anonymous type sequence, each of which contains the attributes of the FOO and BAR table. If the line in the FOO table does not match in the BAR table, the corresponding BAR attribute will be NULL.
The above is the detailed content of How to Perform Left Outer Joins in LINQ Using Extension Methods?. For more information, please follow other related articles on the PHP Chinese website!