Answered by:
Left and right outer join by LINQ

Question
-
User1183902823 posted
see just a example taken from http://www.devcurry.com/2011/01/linq-left-join-example-in-c.html
var orderForBooks = from bk in bookList join ordr in bookOrders on bk.BookID equals ordr.BookID into a from b in a.DefaultIfEmpty(new Order()) select new { bk.BookID, Name = bk.BookNm, b.PaymentMode }; foreach (var item in orderForBooks) Console.WriteLine(item);
normally when we do left or right outer join with sql query then we use left or right keyword to inform sql server that i want left or right join but with linq there is no such keyword used. so how system understand that i am using left or right join ?
please help me to understand how system understand that i am using left or right join ? thanks
Tuesday, November 21, 2017 9:37 AM
Answers
-
User1400794712 posted
Hi tridip1974,
As the code you provided, normally we use the DefaultIfEmpty method on the results of a group join to perform a left/right outer join. This article explained how to perform left outer joins with example, you can check it for detailed information.
Best Regards,
Daisy
<sub></sub><sup></sup>
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, November 22, 2017 8:14 AM -
User-832373396 posted
<g class="gr_ gr_10 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="10" data-gr-id="10">Hi</g> tridip1974,
can show the <g class="gr_ gr_117 gr-alert gr_gramm gr_hide gr_inline_cards gr_run_anim Style multiReplace replaceWithoutSep replaceWithoutSep" id="117" data-gr-id="117">syntactical</g><g class="gr_ gr_117 gr-alert gr_gramm gr_hide gr_inline_cards gr_disable_anim_appear Style multiReplace replaceWithoutSep replaceWithoutSep" id="117" data-gr-id="117"> </g><g class="gr_ gr_117 gr-alert gr_gramm gr_hide gr_inline_cards gr_disable_anim_appear Style multiReplace replaceWithoutSep replaceWithoutSep" id="117" data-gr-id="117">difference</g> between left and right outer join by <g class="gr_ gr_115 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="115" data-gr-id="115">linq</g> with <g class="gr_ gr_116 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="116" data-gr-id="116">example</g>. thanksPerform inner joins:
var query = from person in people join pet in pets on person equals pet.Owner select new { OwnerName = person.FirstName, PetName = pet.Name };
and Perform left outer joins:
(note, you could see that it will <g class="gr_ gr_599 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="599" data-gr-id="599">care </g><g class="gr_ gr_599 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Style multiReplace" id="599" data-gr-id="599">gj</g>.DefaultIfEmpty() and set default value )
var query = from person in people join pet in pets on person equals pet.Owner into gj from subpet in gj.DefaultIfEmpty() select new { person.FirstName, PetName = subpet?.Name ?? String.Empty };
I suggest that you could learn the EF from Microsoft Official Document:
Method-Based Query Syntax Examples: Join Operators
Perform left outer joins
https://docs.microsoft.com/en-us/dotnet/csharp/linq/perform-left-outer-joins
Perform inner joins
https://docs.microsoft.com/en-us/dotnet/csharp/linq/perform-inner-joins
Overview:
https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/ef/overview
More references:
1 Entity Framework (EF) Documentation(Data developer Center)
https://msdn.microsoft.com/en-us/data/ee712907(vs.113)
2 Microsoft Virtual Academy(<g class="gr_ gr_78 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del" id="78" data-gr-id="78">Videoes</g>)
Implementing Entity Framework with MVC
3 MSDN document and videoes
https://msdn.microsoft.com/en-us/library/jj713564(v=vs.113).<g class="gr_ gr_79 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="79" data-gr-id="79">aspx</g>
4 Entityframeworktutorial site
With regards, Angelina Jolie
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, November 22, 2017 9:31 AM -
User-832373396 posted
<g class="gr_ gr_8 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="8" data-gr-id="8">Hi</g> <g class="gr_ gr_5 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="5" data-gr-id="5">tridip</g>,
<g class="gr_ gr_14 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del" id="14" data-gr-id="14">how</g> me the difference between left and right outer join syntax with a small example.In fact, it only changes the order;
The is the test example(LeftOuterJoinExample):
var query = from person in people join pet in pets on person equals pet.Owner into gj from subpet in gj.DefaultIfEmpty()//// the gj represents pet conllection select new { person.FirstName, PetName = subpet?.Name ?? String.Empty };
// here 'PetName = ' is required,because Name property of subpet could null.
foreach (var v in query) { Debug.WriteLine($"{v.FirstName + ":",-15}{v.PetName}"); }The is the test example(RightOuterJoinExample):
var query = from pet in pets join person in people on pet.Owner equals person into gj from subpet in gj.DefaultIfEmpty()// now the gj represents person conllection select new { FirstName= subpet?.FirstName ?? String.Empty, pet.Name };// here 'FirstName= ' is required
foreach (var v in query) { Debug.WriteLine($"{v.Name + ":",-15}{v.FirstName } ");
}Output:
Magnus: Daisy Terry: Barley Terry: Boots Terry: Blue Moon Charlotte: Whiskers Arlene: ------------------------ Barley: Terry Boots: Terry Whiskers: Charlotte Blue Moon: Terry Daisy: Magnus
References:
http://csharp-guide.blogspot.sg/2012/05/linq-to-<g class="gr_ gr_199 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="199" data-gr-id="199">sql</g>-right-outer-join.html
http://csharp-guide.blogspot.sg/2012/05/linq-to-<g class="gr_ gr_219 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="219" data-gr-id="219">sql</g>-left-outer-join.html
With regards, Angelina Jolie
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, November 23, 2017 7:13 AM
All replies
-
User1120430333 posted
I suggest that you use a SQL Profiler and look at the T-SQL generated by the EF engine before it is submitted for execution by the database engine.
Another tool you can use to see the generated T-SQL from a Linq query is LinqPAD.
Tuesday, November 21, 2017 3:52 PM -
User1400794712 posted
Hi tridip1974,
As the code you provided, normally we use the DefaultIfEmpty method on the results of a group join to perform a left/right outer join. This article explained how to perform left outer joins with example, you can check it for detailed information.
Best Regards,
Daisy
<sub></sub><sup></sup>
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, November 22, 2017 8:14 AM -
User1183902823 posted
what would be syntactical difference between left and right outer join with linq.
can show the syntactical difference between left and right outer join by linq with example. thanks
Wednesday, November 22, 2017 8:27 AM -
User-832373396 posted
<g class="gr_ gr_10 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="10" data-gr-id="10">Hi</g> tridip1974,
can show the <g class="gr_ gr_117 gr-alert gr_gramm gr_hide gr_inline_cards gr_run_anim Style multiReplace replaceWithoutSep replaceWithoutSep" id="117" data-gr-id="117">syntactical</g><g class="gr_ gr_117 gr-alert gr_gramm gr_hide gr_inline_cards gr_disable_anim_appear Style multiReplace replaceWithoutSep replaceWithoutSep" id="117" data-gr-id="117"> </g><g class="gr_ gr_117 gr-alert gr_gramm gr_hide gr_inline_cards gr_disable_anim_appear Style multiReplace replaceWithoutSep replaceWithoutSep" id="117" data-gr-id="117">difference</g> between left and right outer join by <g class="gr_ gr_115 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="115" data-gr-id="115">linq</g> with <g class="gr_ gr_116 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="116" data-gr-id="116">example</g>. thanksPerform inner joins:
var query = from person in people join pet in pets on person equals pet.Owner select new { OwnerName = person.FirstName, PetName = pet.Name };
and Perform left outer joins:
(note, you could see that it will <g class="gr_ gr_599 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="599" data-gr-id="599">care </g><g class="gr_ gr_599 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Style multiReplace" id="599" data-gr-id="599">gj</g>.DefaultIfEmpty() and set default value )
var query = from person in people join pet in pets on person equals pet.Owner into gj from subpet in gj.DefaultIfEmpty() select new { person.FirstName, PetName = subpet?.Name ?? String.Empty };
I suggest that you could learn the EF from Microsoft Official Document:
Method-Based Query Syntax Examples: Join Operators
Perform left outer joins
https://docs.microsoft.com/en-us/dotnet/csharp/linq/perform-left-outer-joins
Perform inner joins
https://docs.microsoft.com/en-us/dotnet/csharp/linq/perform-inner-joins
Overview:
https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/ef/overview
More references:
1 Entity Framework (EF) Documentation(Data developer Center)
https://msdn.microsoft.com/en-us/data/ee712907(vs.113)
2 Microsoft Virtual Academy(<g class="gr_ gr_78 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del" id="78" data-gr-id="78">Videoes</g>)
Implementing Entity Framework with MVC
3 MSDN document and videoes
https://msdn.microsoft.com/en-us/library/jj713564(v=vs.113).<g class="gr_ gr_79 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="79" data-gr-id="79">aspx</g>
4 Entityframeworktutorial site
With regards, Angelina Jolie
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, November 22, 2017 9:31 AM -
User1183902823 posted
thanks for your answer but you missed right outer join example because i asked what would be syntactical difference between left and right outer join with linq EF.
so show me the difference between left and right outer join syntax with a small example. thanks
Wednesday, November 22, 2017 9:45 AM -
User-832373396 posted
<g class="gr_ gr_8 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="8" data-gr-id="8">Hi</g> <g class="gr_ gr_5 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="5" data-gr-id="5">tridip</g>,
<g class="gr_ gr_14 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del" id="14" data-gr-id="14">how</g> me the difference between left and right outer join syntax with a small example.In fact, it only changes the order;
The is the test example(LeftOuterJoinExample):
var query = from person in people join pet in pets on person equals pet.Owner into gj from subpet in gj.DefaultIfEmpty()//// the gj represents pet conllection select new { person.FirstName, PetName = subpet?.Name ?? String.Empty };
// here 'PetName = ' is required,because Name property of subpet could null.
foreach (var v in query) { Debug.WriteLine($"{v.FirstName + ":",-15}{v.PetName}"); }The is the test example(RightOuterJoinExample):
var query = from pet in pets join person in people on pet.Owner equals person into gj from subpet in gj.DefaultIfEmpty()// now the gj represents person conllection select new { FirstName= subpet?.FirstName ?? String.Empty, pet.Name };// here 'FirstName= ' is required
foreach (var v in query) { Debug.WriteLine($"{v.Name + ":",-15}{v.FirstName } ");
}Output:
Magnus: Daisy Terry: Barley Terry: Boots Terry: Blue Moon Charlotte: Whiskers Arlene: ------------------------ Barley: Terry Boots: Terry Whiskers: Charlotte Blue Moon: Terry Daisy: Magnus
References:
http://csharp-guide.blogspot.sg/2012/05/linq-to-<g class="gr_ gr_199 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="199" data-gr-id="199">sql</g>-right-outer-join.html
http://csharp-guide.blogspot.sg/2012/05/linq-to-<g class="gr_ gr_219 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="219" data-gr-id="219">sql</g>-left-outer-join.html
With regards, Angelina Jolie
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, November 23, 2017 7:13 AM