Anonymus Types:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Demos
{
class clsAnonymasTypes
{
static void Main()
{
var person = new {
name = "Venu gopal", age = 27 };
/* // Basic creation of Anonymus type
Console.WriteLine("{0},{1}",person.name,person.age);
Console.ReadLine();
*/
// step 2
//Instantiate the Employee list
// List<Employee> empList = new
List<Employee>();
List<Employee>
empList = new List<Employee> { new
Employee { EmpNo = 1013, EmpName = "Umakar", Dept = "Student", Salary = 15000 },
new Employee
{ EmpNo = 1010, EmpName = "Venu",
Dept = "HR", Salary = 12000 } };
empList.Add(new Employee
{ EmpNo = 1010, EmpName = "Venu",
Dept = "SWE", Salary = 12000 });
empList.Add(new Employee
{ EmpNo = 1011, EmpName = "srinu",
Dept = "CISF", Salary = 13000 });
empList.Add(new Employee
{ EmpNo = 1012, EmpName = "Shekar",
Dept = "BSF", Salary = 14000 });
//step 3
//Var can store all type of data
//In Case 1: it wil
store the object(Containing all type "int,string,float" etc)
//In Case 2: it will stoire the data of type
"string" Only
//In Case
3: It will filter and store only two columns of type "string" and
"Int"
var emps = from e in empList
//case
1: we can select all column by writing" select e"
//
case 2: if we want to select only Name from list, we write as "select e.EmpName"
//
case 3: if We want to slect a specified
columns from the list, we write as "select new{ column1,column 2}"
//we
can chage the column naes also as per requirement
select
new { EmployeeName = e.EmpName, EmployeeSalary
= e.Salary }
;
//Using Lamda Expression
var emp1 = empList.Select(e => new { e.EmpName, e.Salary });
//To get a sum
of salaries
var sum = empList.Sum(e => e.Salary);
//Step 4
foreach (var data in emps)
{
Console.WriteLine("{0},{1}", data.EmployeeName,
data.EmployeeSalary);
}
Console.ReadLine();
}
}
class Employee
{
//Step 1(a)
public int EmpNo { get; set; }
public string EmpName
{ get; set; }
public string Dept { get; set; }
public int Salary { get; set; }
//Step 1(b)
//Wew can OverRide the Default methods as per requirement
public override string ToString()
{
return string.Format("{0},{1},{2},{3}", EmpNo, EmpName, Dept,
Salary);
}
}
}
No comments:
Post a Comment