There are currently two tables, Sgroup and Sgroupuser, which are associated by Gkey, while the Sgroup table records the group, and the Sgroupuser record is the user in the group, so there is no data in the Sgroupuser. You need to use the left join to get the data:
The LINQ syntax is as follows:
var sg = ( from the dc.sgroup in dc.sgroupuser on G.gkey equals Gu.gkey into L from in l.defaultifempty () SelectNew {g, LGU}). ToList ();
The lambda expression is as follows:
var New {g, gu}). Select (O=>o). ToList ();
Attention:
LINQ has a different result than a lambda expression. The number of records that LINQ takes out is the same as the result of a left join in SQL, and the number of records taken by a lambda expression is the number of records in the Sgroup table. Sgroupuser corresponding records are present in the result with the object collection
Report:
Here is the inner Join:
The LINQ syntax is as follows:
var sg = ( from the dc.sgroup in dc.sgroupuser on G.gkey equals Gu.gkey Selectnew {g, gu}). ToList ();
The lambda expression is as follows:
var New {g, gu}). Select (O=>o). ToList ();
Attention:
The ToList () method is used at the end, and ToList () is used to fetch the data locally at once.
Implementing a LEFT join with a inner join in LINQ to SQL using LINQ syntax with lambda expressions