Professional Documents
Culture Documents
It Is An Intimation That Following Lectures Are Not Going To Repeat and Would Be Part of Mid-Term & Final Exam As Well
It Is An Intimation That Following Lectures Are Not Going To Repeat and Would Be Part of Mid-Term & Final Exam As Well
pk WhatsApp# 0315-5425199
Lecture 23-24
Note:
It is an intimation that following Lectures are not going to repeat and would
be part of mid-term & final exam as well.
Here is the YouTube link for the video explanation of the lecture:
https://youtu.be/zN09C3K08aQ
Here are links of same Video but in four (04) parts (Google Drive), in case of internet speed issue
https://drive.google.com/open?id=1t152fLONrll1U3DjPGjwMXaMeH5GSWew
https://drive.google.com/open?id=1QMTlJdKDVfdYE6BIrhvAKz9UkZ73cugL
https://drive.google.com/open?id=1lMaN4R_ipvgmHyk8giabQasVBG-tWUa-
https://drive.google.com/open?id=1kASBC90rIlgP_nWqzzpLDD1ga7aRI2cg
1
Mr. Shahid Jamil email id: shahid@biit.edu.pk WhatsApp# 0315-5425199
Lecture 23-24
//Define columns
var cId = new DataColumn("Id", Type.GetType("System.Int32"));
var cDepName = new DataColumn("DName", Type.GetType("System.String"));
//////
//Declare Data
var dtEmployees = new DataTable();
2
Mr. Shahid Jamil email id: shahid@biit.edu.pk WhatsApp# 0315-5425199
Lecture 23-24
var cName = new DataColumn("Name", Type.GetType("System.String"));
var cGender = new DataColumn("Gender", Type.GetType("System.Char")); //e.g.
'F' or 'M'
var cDob = new DataColumn("Dob", Type.GetType("System.DateTime"));
var cSalary = new DataColumn("Salary", Type.GetType("System.Int32"));
var cCity = new DataColumn("City", Type.GetType("System.String"));
var cDeptId = new DataColumn("DeptId", Type.GetType("System.Int32"));
dtEmployees.Columns.Add(cName);
dtEmployees.Columns.Add(cGender);
dtEmployees.Columns.Add(cDob);
dtEmployees.Columns.Add(cSalary);
dtEmployees.Columns.Add(cCity);
dtEmployees.Columns.Add(cDeptId);
var query1 =
dtDepartments.AsEnumerable().GroupJoin(dtEmployees.AsEnumerable(),
dpt =>
dpt.Field<Int32>("Id"),
emp =>
emp.Field<Int32>("DeptId"),
(dpt,emps) => new
3
Mr. Shahid Jamil email id: shahid@biit.edu.pk WhatsApp# 0315-5425199
Lecture 23-24
{
DptName =
dpt.Field<String>("DName"),
Employees = emps//
IEnumerable<DataRow>
});
Console.WriteLine("Result of GroupJoin for Employees and Departments");
foreach (var g in query1)
{
Console.WriteLine("------------");
Console.WriteLine(g.DptName);
Console.WriteLine("-----------");
foreach (var emp in g.Employees)
{
Console.WriteLine(emp.Fields());
}
DeptName=d.Field<string>("DName"),
Employee = e// DataRow
});
//Inner Join of Department and Employee, commonly known as Join
Console.WriteLine("Inner Join of Department and Employee");
foreach (var g in query2)
{
Console.WriteLine("{0}::{1}",g.DeptName,g.Employee.Fields());
}
4
Mr. Shahid Jamil email id: shahid@biit.edu.pk WhatsApp# 0315-5425199
Lecture 23-24
d => d.Field<int>("Id"),
(e, d) => new
{
Employee = e,
Depatments = d
})
.SelectMany(v =>
v.Depatments.DefaultIfEmpty(),
(a, b) => new
{
Name =
a.Employee.Field<string>("Name"),
Dob =
a.Employee.Field<DateTime>("Dob"),
Dept = b == null?
"...":b.Field<string>("DName")
});
foreach (var v in query3)
{
Console.WriteLine(v);
}
Employees.xml
<Employees>
<Employee>
<Name>Qasim</Name>
<Gender>M</Gender>
<Dob>25-Nov-1998</Dob>
<Salary>45000</Salary>
<City>Rawalpindi</City>
<DeptId>1</DeptId>
</Employee>
<Employee>
<Name>Fatima</Name>
<Gender>F</Gender>
<Dob>12-10-1995</Dob>
<Salary>35000</Salary>
<City>Lahore</City>
<DeptId>2</DeptId>
</Employee>
<Employee>
<Name>Nasir</Name>
<Gender>M</Gender>
<Dob>09-Jan-1993</Dob>
<Salary>85000</Salary>
<City>Lahore</City>
<DeptId>1</DeptId>
</Employee>
<Employee>
<Name>Manzoor</Name>
5
Mr. Shahid Jamil email id: shahid@biit.edu.pk WhatsApp# 0315-5425199
Lecture 23-24
<Gender>M</Gender>
<Dob>15-Sep-1990</Dob>
<Salary>35000</Salary>
<City>Islamabad</City>
<DeptId>3</DeptId>
</Employee>
<Employee>
<Name>Khalid</Name>
<Gender>M</Gender>
<Dob>01-02-2001</Dob>
<Salary>25000</Salary>
<City>Lahore</City>
<DeptId>1</DeptId>
</Employee>
<Employee>
<Name>Ayesha</Name>
<Gender>F</Gender>
<Dob>10-09-1992</Dob>
<Salary>60000</Salary>
<City>Rawalpindi</City>
<DeptId>2</DeptId>
</Employee>
<Employee>
<Name>Ahmed</Name>
<Gender>M</Gender>
<Dob>10-09-1997</Dob>
<Salary>30000</Salary>
<City>Rawalpindi</City>
<DeptId>1</DeptId>
</Employee>
<Employee>
<Name>Sana</Name>
<Gender>F</Gender>
<Dob>09-02-2002</Dob>
<Salary>47000</Salary>
<City>Rawalpindi</City>
<DeptId>3</DeptId>
</Employee>
<Employee>
<Name>Zulfiqar</Name>
<Gender>M</Gender>
<Dob>17-06-1995</Dob>
<Salary>34000</Salary>
<City>Islamabad</City>
<DeptId>2</DeptId>
</Employee>
<Employee>
<Name>Zaheer</Name>
<Gender>M</Gender>
<Dob>10-02-1999</Dob>
<Salary>18000</Salary>
<City>Lahore</City>
6
Mr. Shahid Jamil email id: shahid@biit.edu.pk WhatsApp# 0315-5425199
Lecture 23-24
<DeptId>3</DeptId>
</Employee>
</Employees>
Departments.xml
<Departments>
<Department Id="1">
<Name>HR</Name>
</Department>
<Department Id="2">
<Name>Marketing</Name>
</Department>
<Department Id="3">
<Name>Accounts</Name>
</Department>
</Departments>
Students.xml
<Students>
<Student Id="100" Gender="M">
<Name>Ali</Name>
<Discipline>BSCS</Discipline>
</Student>
Here is XElement:
<Name>Zaheer</Name>
OR
<Employee>
7
Mr. Shahid Jamil email id: shahid@biit.edu.pk WhatsApp# 0315-5425199
Lecture 23-24
<Name>Zaheer</Name>
<Gender>M</Gender>
<Dob>10-02-1999</Dob>
<Salary>18000</Salary>
<City>Lahore</City>
<DeptId>3</DeptId>
</Employee>
Here is the code stuff that how we will load xml document:
Aggregation – XML
We can also apply aggregation on XML file. Here are examples of Count, Min, Max, Avg, and Sum
//3.1 Aggregations
8
Mr. Shahid Jamil email id: shahid@biit.edu.pk WhatsApp# 0315-5425199
Lecture 23-24
var query1 = allEmployees.Where(e => char.Parse(e.Element("Gender").Value) ==
'M');
Console.WriteLine("All Male Employees\n");
foreach (XElement emp in query1)
{
//Console.WriteLine(emp.Element("Name").Value);
Console.WriteLine(emp.Fields());
}
Console.ReadKey();
Console.Clear();
Console.ReadKey();
Console.Clear();
9
Mr. Shahid Jamil email id: shahid@biit.edu.pk WhatsApp# 0315-5425199
Lecture 23-24
10
Mr. Shahid Jamil email id: shahid@biit.edu.pk WhatsApp# 0315-5425199
Lecture 23-24
Assignment
Q1. In week-11 we applied two methods of Cross Join, Now you need to apply both methods
on two DataTables (i.e. dtEmployees and dtDepartments) and print the result.
Q2. For LINQ to XML you need to apply GroupJoin, Left Outer Join and Cross Join(Employees
and Departments).
11