﻿ C# Sharp Exercises: LINQ : Generate an Inner Join between two data sets - w3resource

# C# Sharp Exercises: LINQ : Generate an Inner Join between two data sets

## C# Sharp LINQ : Exercise-25 with Solution

Write a program in C# Sharp to generate an Inner Join between two data sets.

Sample Solution:-

C# Sharp Code:

``````using System;
using System.Linq;
using System.Collections.Generic;

class  LinqExercise25
{
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 = 2,  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 an Inner 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 innerJoin = from e in itemlist
join d in purchlist on e.ItemId equals d.ItemId
select new
{
itid = e.ItemId,
itdes = e.ItemDes,
prqty = d.PurQty
};
Console.WriteLine("Item ID\t\tItem Name\tPurchase Quantity");
Console.WriteLine("-------------------------------------------------------");
foreach (var data in innerJoin)
{
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 an Inner 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 : 2,  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
-------------------------------------------------------
1               Biscuit                 458
2               Chocolate               650
3               Butter                  800
3               Butter                  900
3               Butter                  900
4               Brade                   700
4               Brade                   650
```

Flowchart:

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

﻿

New Content: Composer: Dependency manager for PHP, R Programming