28 December 2012

LINQ To Object -- Example 1

 Count of Rows in the Table using LINQ
1) Create a One Class Called : ClsEmployee
Three Properties in the Class.
1)EmpId2)Name3)Salary
Code :
   public string Name { get; set; }
   public string EmpID { get; set; }
   public int Salary { get; set; }
2)  Create a One form
Drag and Drop one Button.
Button name is btn_Result.
Create a Method:

Method name is "InsertintoTable()
   List<ClsEmployees> objEmployee = new List<ClsEmployees>
            {
                new ClsEmployees { EmpID = "1001" ,Name ="GANTEC", Salary =10000 },
                new ClsEmployees { EmpID = "1002" ,Name ="IBM", Salary =15000 },
                new ClsEmployees { EmpID = "1003" ,Name ="CTS", Salary =20000 },
                new ClsEmployees { EmpID = "1004" ,Name ="WIPRO", Salary =30000 },
                new ClsEmployees { EmpID = "1005" ,Name ="SATHYAM", Salary =40000 },
                new ClsEmployees { EmpID = "1006" ,Name ="BUTTERFLY", Salary =50000 },
                new ClsEmployees { EmpID = "1007" ,Name ="CON", Salary =10000 },
                new ClsEmployees { EmpID = "1008" ,Name ="TVS", Salary =75000 },
                 new ClsEmployees { EmpID = "1010" ,Name ="TATA", Salary =90000 },
                 new ClsEmployees { EmpID = "1009" ,Name ="GANTEC COR", Salary =100000 },
             };
Call this method in button Click Event and Add below codes,
 var CountofRows = (from emp in objEmployee select emp.Salary).Count();
 MessageBox.Show("Total Records in Employee Table :" + CountofRows );
Result: "Total Records in Employee Table: 10" in the Message Box

No comments: