SELECT AX, BY FROM A JOIN B ON AX = BY
This call to the linq (to Join) method will generate the above Join.
var query = A.Join ( B, a => ax, b => by, (a, b) => new {ax, by}
SELECT AX, BY FROM A LEFT JOIN B ON AX = BY
These linq method calls (for GroupJoin, SelectMany, DefaultIfEmpty) will call the above Left Join
var query = A.GroupJoin ( B, a => ax, b => by, (a, g) => new {a, g} ).SelectMany ( z => zgDefaultIfEmpty(), (z, b) => new { x = zax, y = by }
The key concept here is that Linq methods produce results in a hierarchical form, rather than flattened row column forms.
- Linq
GroupBy creates results formed in a hierarchy with a grouping key mapped to a collection of elements (which may be non-empty). SQL GroupBy creates a grouping key with aggregated values - no, to work with a sub-collection. - Similarly, Linq
GroupJoin creates a hierarchical form β a parent record that maps to a collection of child records (which may be empty). Sql LEFT JOIN creates a parent record corresponding to each child record, or a zero child record if there are no other matches. To go to the Sql form from the Linq form, you need to unzip the collection of child records using SelectMany and process the empty collections of child records using DefaultIfEmpty .
And here is my attempt to eliminate this sql in the question:
var query = from a in Appointment where a.RowStatus == 1 where a.Type == 1 from b in a.AppointmentFormula.Where(af => af.RowStatus == 1).DefaultIfEmpty() from d in a.TypeRecord
Amy b
source share