PDA

View Full Version : سورس کامل تمرین LINQ



zkazemi
دوشنبه 19 مرداد 1388, 13:39 عصر
اینم سورس کامل تمرین LINQ که تو تاپیک قبل بررسی شد:





using System;
using System.Collections.Generic;
using System.Linq;
namespace ITCompany
{
class Program
{
static void Main(string[] args)
{
//Create a list of Employees
List LEmp1 = new List();
LEmp1.Add(new Employee() { Name = "Emp01", Age = 24, salary = 40000, Address = new Address() { PinCode = 123451, City = "Bangalore" }});
LEmp1.Add(new Employee() { Name = "Emp02", Age = 25, salary = 35000, Address = new Address() { PinCode = 123452, City = "Delhi" } });
LEmp1.Add(new Employee() { Name = "Emp03", Age = 24, salary = 22000, Address = new Address() { PinCode = 123453, City = "Bombay" } });
LEmp1.Add(new Employee() { Name = "Emp04", Age = 25, salary = 14000, Address = new Address() { PinCode = 123454, City = "Chennai" } });
LEmp1.Add(new Employee() { Name = "Emp05", Age = 26, salary = 25000, Address = new Address() { PinCode = 123455, City = "Bangalore" } });
Company cmp1 = new Company() { Name = "Relyon", ListEmp = LEmp1};
List LEmp2 = new List();
LEmp2.Add(new Employee() { Name = "Emp07", Age = 25, salary = 35000, Address = new Address() { PinCode = 123456, City = "Bangalore" } });
LEmp2.Add(new Employee() { Name = "Emp08", Age = 24, salary = 15000, Address = new Address() { PinCode = 123457, City = "Bombay" } });
LEmp2.Add(new Employee() { Name = "Emp09", Age = 25, salary = 10000, Address = new Address() { PinCode = 123458, City = "Delhi" } });
LEmp2.Add(new Employee() { Name = "Emp10", Age = 26, salary = 25000, Address = new Address() { PinCode = 123459, City = "Bangalore" } });
Company cmp2 = new Company() { Name = "CellOn" , ListEmp = LEmp2};
Company[] ListCompany = { cmp1, cmp2 };
//
//Employee Details



//



Console.WriteLine("--------------------------------------------------------------------");



Console.WriteLine("Employee Details");



Console.WriteLine("--------------------------------------------------------------------");



Console.WriteLine("Company | Name | Age | Salary | Address |");



Console.WriteLine("--------------------------------------------------------------------");




var EmpDetails = from comp in ListCompany



select new



{



Emp = (from emp in comp.ListEmp



select new { Company = comp.Name, emp })



};




foreach (var t in EmpDetails)



{



Console.WriteLine(string.Join("\n", t.Emp.Select(emp => emp.Company + " |" + emp.emp.Name + " |" + emp.emp.Age + " |" + emp.emp.salary + " |" + emp.emp.Address.City + "-" + emp.emp.Address.PinCode).ToArray()));



}




Console.WriteLine();



Console.WriteLine();




//



//company with Employee count



//



Console.WriteLine("--------------------------------------------------");



Console.WriteLine("Nos of employees");



Console.WriteLine("--------------------------------------------------");




var LessEmp = from Comp in ListCompany



select new



{



Comp.Name,



EmpCount = Comp.ListEmp.Count



};




foreach(var t in LessEmp)



{



Console.WriteLine("Company Name : " + t.Name + ", Nos Of Employees : " + t.EmpCount);



}



Console.WriteLine(); Console.WriteLine();




//



//Employees who are staying in BANGALORE



//



Console.WriteLine("--------------------------------------------------");



Console.WriteLine("Employees statying in bangalore");



Console.WriteLine("--------------------------------------------------");




var EmpInACity = from comp in ListCompany



from emplist in comp.ListEmp



where emplist.Address.City.ToUpper().Contains("BAN")



select new { CompName = comp.Name, EmployeeName = emplist.Name };




foreach (var t in EmpInACity)



{



Console.WriteLine("Company Name : " + t.CompName + ", Employee Name : " + t.EmployeeName);



}



Console.WriteLine(); Console.WriteLine();




//



//Employee with Hightest salary in each company



//



Console.WriteLine("--------------------------------------------------");



Console.WriteLine("Highest paid employee in each company");



Console.WriteLine("--------------------------------------------------");




var EmpHighSalEachComp = from comp in ListCompany



from empHigh in comp.ListEmp



where empHigh.salary == comp.ListEmp.Max(HighEmp => HighEmp.salary)



select new { CompanyName = comp.Name, EmpHighName = empHigh.Name, EmpHighSal = empHigh.salary};




foreach (var t in EmpHighSalEachComp)



{



Console.WriteLine("Company : " + t.CompanyName + ", Employee : " + t.EmpHighName + ", Salary : " + t.EmpHighSal);



}



Console.WriteLine(); Console.WriteLine();




//



//Employee with Hightest salary



//



Console.WriteLine("--------------------------------------------------");



Console.WriteLine("Highest paid employee from all companies");



Console.WriteLine("--------------------------------------------------");




var EmpHighSal = from comp in ListCompany



from emp in comp.ListEmp



where emp.salary == ListCompany.Max(TComp => TComp.ListEmp.Max(HighEmp => HighEmp.salary))



select new { CompanyName = comp.Name , EmployeeName = emp.Name, EmpSal = emp.salary};




foreach (var t in EmpHighSal)



{



Console.WriteLine("Company : " + t.CompanyName + ", Employee : " + t.EmployeeName + ", Salary : " + t.EmpSal);



}



Console.WriteLine(); Console.WriteLine();




//



//Salary Paid in Each City together by all companies



//



Console.WriteLine("--------------------------------------------------");



Console.WriteLine("Payment done in cities together by all companies");



Console.WriteLine("--------------------------------------------------");




var CompanyCityWise = from comp in ListCompany



from emp in comp.ListEmp



group emp by emp.Address.City into CityWiseEmp



select new { State = CityWiseEmp.Key, TotalSalary = CityWiseEmp.Sum(emp => emp.salary) };




foreach (var t in CompanyCityWise)



{



Console.WriteLine("City : " + t.State + ", " + "Total Salary : " + t.TotalSalary);



}



Console.WriteLine(); Console.WriteLine();




//



//Salary Paid in Each City by each company



//



Console.WriteLine("--------------------------------------------------");



Console.WriteLine("Payment done in cities by each company");



Console.WriteLine("--------------------------------------------------");




var CityWiseSalary = from comp in ListCompany



select new



{



comp.Name,



Emp =(



from emp in comp.ListEmp



group emp by emp.Address.City into CityWiseEmp



select new { State = CityWiseEmp.Key, TotalSalary = CityWiseEmp.Sum(emp => emp.salary) })



};




foreach (var t in CityWiseSalary)



{



//t.emp comes as a structure so to easy the printing



//we will use string.join to join all the values in a single string



//It can also be done by looping through t.emp and



//printing t.emp.state and t.emp.totalsalary separately




Console.WriteLine("company : " + t.Name + "\n" + string.Join("\n", t.Emp.Select(emp => emp.State + " : " + emp.TotalSalary).ToArray()));



Console.WriteLine(); Console.WriteLine();



}



Console.WriteLine(); Console.WriteLine();





Console.Read();



}



}




public class Company



{



public string Name



{ get; set; }




public List ListEmp



{ get; set; }



}




public class Employee



{



public string Name



{ get; set; }




public Address Address



{ get; set; }




public int Age



{ get; set; }




public double salary



{ get; set; }



}




public struct Address



{



public int PinCode



{ get; set; }



public string City



{ get; set; }



}



}