C# Sharp Exercises: Generate a Right Join between two data sets
C# Sharp LINQ : Exercise-27 with Solution
Write a program in C# Sharp to generate a Right Outer Join between two data sets.
Sample Solution:-
C# Sharp Code:
//LINQ does not supports right outer join, only supports left outer joins.
//You can get the behavior of a right outer join if you swap the tables and do a left outer join.
using System;
using System.Linq;
using System.Collections.Generic;
class LinqExercise27
{
static void Main(string[] args)
{
List<Item_mast> itemlist = new List<Item_mast>
{
new Item_mast { ItemId = 1, ItemDes = "Biscuit " },
new Item_mast { ItemId = 2, ItemDes = "Chocolate" },
new Item_mast { ItemId = 3, ItemDes = "Butter " },
new Item_mast { ItemId = 4, ItemDes = "Brade " },
new Item_mast { ItemId = 5, ItemDes = "Honey " }
};
List<Purchase> purchlist = new List<Purchase>
{
new Purchase { InvNo=100, ItemId = 3, PurQty = 800 },
new Purchase { InvNo=101, ItemId = 5, PurQty = 650 },
new Purchase { InvNo=102, ItemId = 3, PurQty = 900 },
new Purchase { InvNo=103, ItemId = 4, PurQty = 700 },
new Purchase { InvNo=104, ItemId = 3, PurQty = 900 },
new Purchase { InvNo=105, ItemId = 4, PurQty = 650 },
new Purchase { InvNo=106, ItemId = 1, PurQty = 458 }
};
Console.Write("\nLINQ : Generate a Right Join between two data sets : ");
Console.Write("\n--------------------------------------------------\n");
Console.Write("Here is the Item_mast List : ");
Console.Write("\n-------------------------\n");
foreach (var item in itemlist)
{
Console.WriteLine(
"Item Id: {0}, Description: {1}",
item.ItemId,
item.ItemDes);
}
Console.Write("\nHere is the Purchase List : ");
Console.Write("\n--------------------------\n");
foreach (var item in purchlist)
{
Console.WriteLine(
"Invoice No: {0}, Item Id : {1}, Quantity : {2}",
item.InvNo,
item.ItemId,
item.PurQty);
}
Console.Write("\nHere is the list after joining : \n\n");
var rightOuterJoin = from p in purchlist
join i in itemlist
on p.ItemId equals i.ItemId
into a
from b in a.DefaultIfEmpty()
select new
{
itid=b.ItemId,
itdes = b.ItemDes,
prqty=p.PurQty
};
Console.WriteLine("Item ID\t\tItem Name\tPurchase Quantity");
Console.WriteLine("-------------------------------------------------------");
foreach (var data in rightOuterJoin )
{
Console.WriteLine(data.itid + "\t\t" + data.itdes + "\t\t" + data.prqty);
}
Console.ReadLine();
}
}
public class Item_mast
{
public int ItemId { get; set; }
public string ItemDes { get; set; }
}
public class Purchase
{
public int InvNo { get; set; }
public int ItemId { get; set; }
public int PurQty { get; set; }
}
Sample Output:
LINQ : Generate a Right Join between two data sets : -------------------------------------------------- Here is the Item_mast List : ------------------------- Item Id: 1, Description: Biscuit Item Id: 2, Description: Chocolate Item Id: 3, Description: Butter Item Id: 4, Description: Brade Item Id: 5, Description: Honey Here is the Purchase List : -------------------------- Invoice No: 100, Item Id : 3, Quantity : 800 Invoice No: 101, Item Id : 5, Quantity : 650 Invoice No: 102, Item Id : 3, Quantity : 900 Invoice No: 103, Item Id : 4, Quantity : 700 Invoice No: 104, Item Id : 3, Quantity : 900 Invoice No: 105, Item Id : 4, Quantity : 650 Invoice No: 106, Item Id : 1, Quantity : 458 Here is the list after joining : Item ID Item Name Purchase Quantity ------------------------------------------------------- 3 Butter 800 5 Honey 650 3 Butter 900 4 Brade 700 3 Butter 900 4 Brade 650 1 Biscuit 458
Flowchart:
C# Sharp Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a program in C# Sharp to generate a Left Join between two data sets.
Next: Write a program in C# Sharp to display the list of items in the array according to the length of the string then by name in ascending order.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework