C#: 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:
using System;
using System.Linq;
using System.Collections.Generic;
class LinqExercise27
{
static void Main(string[] args)
{
// Initializing a list of Item_mast objects
List<Item_mast> itemlist = new List<Item_mast>
{
// Populating Item_mast objects with ItemId and ItemDes properties
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 " }
};
// Initializing a list of Purchase objects
List<Purchase> purchlist = new List<Purchase>
{
// Populating Purchase objects with InvNo, ItemId, and PurQty properties
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 }
};
// Displaying the Item_mast list
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)
{
// Displaying ItemId and ItemDes of each Item_mast object
Console.WriteLine(
"Item Id: {0}, Description: {1}",
item.ItemId,
item.ItemDes);
}
// Displaying the Purchase list
Console.Write("\nHere is the Purchase List : ");
Console.Write("\n--------------------------\n");
foreach (var item in purchlist)
{
// Displaying InvNo, ItemId, and PurQty of each Purchase object
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");
// Performing a right outer join by swapping the tables and using a left outer join
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
};
// Displaying the results of the right outer join
Console.WriteLine("Item ID\t\tItem Name\tPurchase Quantity");
Console.WriteLine("-------------------------------------------------------");
foreach (var data in rightOuterJoin)
{
// Displaying ItemId, ItemDes, and PurQty after the right outer join operation
Console.WriteLine(data.itid + "\t\t" + data.itdes + "\t\t" + data.prqty);
}
Console.ReadLine();
}
}
// Class representing the Item_mast object
public class Item_mast
{
public int ItemId { get; set; } // Property for Item ID
public string ItemDes { get; set; } // Property for Item Description
}
// Class representing the Purchase object
public class Purchase
{
public int InvNo { get; set; } // Property for Invoice Number
public int ItemId { get; set; } // Property for Item ID
public int PurQty { get; set; } // Property for Purchase Quantity
}
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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/csharp-exercises/linq/csharp-linq-exercise-27.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics