Introduction
In the last article, I have explained, how to work on collection with the help of "Linq", Now its time to move towards some new thing about "Linq", I think you all know about the "properties".
Here, I am giving an example to explain "Linq" and "Properties" breifly.
Just, follow the following steps, and make fun with "properties" and "Linq".
Step 1
In VS, File->New->Project->ClassLibrary(take name of it like "student.cs")->ok
Step 2
Write code like:
Here I simply create properties for student class.
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
student
{
public
class Class1
{
private
int stuRollNo;
private
string stuName;
private
int stuAge;
public
int StuAge
{
get
{ return stuAge; }
set
{ stuAge = value; }
}
public
string StuName
{
get
{ return stuName; }
set
{ stuName = value; }
}
public
int StuRollNo
{
get
{ return stuRollNo; }
set
{ stuRollNo = value; }
}
}
}
Step 3After, doing it, I want to use of all the above declared properties , you store the many no. of students records, and do some operation on these stored student records in the properties
like:
1- I want to select the records of that student which have "3" Roll No.
2- I want to select the records of that student which have "Any thing" Name.
and many more things.
So I think, at this time many more things are coming in your mind, I can do this one way or another way. But all the ways to solve the above problems taking line of code and it may be your code complexity will be increase.
So I discussed previously, Linq query "800" times faster than the general query. and to solve all the above problem with the help of Linq taking less time and less complexity and manage with easy code.
Step 1:
File-New->Windows Form( write name of it)->ok and add the above class library (student.cs).
Step 2:
On Form Load
private
void Form1_Load(object
sender, EventArgs e)
{
stuar.Add(new
Class1() { StuName =
"Prince", StuRollNo = 1, StuAge = 22 });
stuar.Add(new
Class1() { StuName =
"Arvind", StuRollNo = 2, StuAge = 22 });
stuar.Add(new
Class1() { StuName =
"Sharad", StuRollNo = 3, StuAge = 24 });
stuar.Add(new
Class1() { StuName =
"Pramod", StuRollNo = 4, StuAge = 25 });
stuar.Add(new
Class1() { StuName =
"Dinesh", StuRollNo = 5, StuAge = 23 });
stuar.Add(new
Class1() { StuName =
"Ravi", StuRollNo = 6, StuAge = 25 });
stuar.Add(new
Class1() { StuName =
"Sachin", StuRollNo = 7, StuAge = 23 });
}
General Experssion
Ques->I want to select the records of that student
which have "5" Roll No.
Here OfType() is used For the element on the specified Type.
var
res = from sturecords
in stuar.OfType<Class1>()
where (sturecords.StuRollNo == 5)
select (sturecords);
foreach
(Class1 stures in
res)
{
MessageBox.Show("Roll No: " +
stures.StuRollNo + Environment.NewLine +
"Name: " + stures.StuName +
Environment.NewLine +
"Age :" + stures.StuAge);
}
Output1:
Lambda Expression
Ques->I want to select the records of that students
which have grater than "6" Roll No.
Ans:
var
res = stuar.OfType<Class1>().Where(sturecords
=> sturecords.StuRollNo > 6).Select(sturecords => sturecords);
foreach
(Class1 stures in
res)
{
MessageBox.Show("Roll No: " +
stures.StuRollNo + Environment.NewLine +
"Name: " + stures.StuName +
Environment.NewLine +
"Age :" + stures.StuAge);
}
Output2:
Anonymous Method
Ques->I want to select the records of that students
which have grater than "5" Roll No.
Ans: In it, I create three anonymous methods, one for where
condition (whe), second for orderby caluse (ord) and last
one for select statement ( sel).
Func<Class1,
bool> whe = delegate(Class1
obj)
{
return
obj.StuRollNo > 5;
};
Func<Class1,
int> ord = delegate(Class1
obj)
{
return
(Int32)obj.StuRollNo;
};
Func<Class1,
Class1> sel =
delegate(Class1 obj)
{
return
obj;
};
private
void button3_Click(object
sender, EventArgs e)
{
var
res = stuar.OfType<Class1>().Where(whe).OrderBy(ord).Select(sel);
foreach
(Class1 stures in
res)
{
MessageBox.Show("Roll No: " +
stures.StuRollNo + Environment.NewLine +
"Name: " + stures.StuName +
Environment.NewLine +
"Age :" + stures.StuAge);
}
}
Output:3
Full Code:
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
using
System.Collections;
namespace
WindowsFormsApplication1
{
public
partial class
Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
ArrayList stuar= new
ArrayList();
private
void Form1_Load(object
sender, EventArgs e)
{
stuar.Add(new
Class1() { StuName =
"Prince", StuRollNo = 1, StuAge = 22 });
stuar.Add(new
Class1() { StuName =
"Arvind", StuRollNo = 2, StuAge = 22 });
stuar.Add(new
Class1() { StuName =
"Sharad", StuRollNo = 3, StuAge = 24 });
stuar.Add(new
Class1() { StuName =
"Pramod", StuRollNo = 4, StuAge = 25 });
stuar.Add(new
Class1() { StuName =
"Dinesh", StuRollNo = 5, StuAge = 23 });
stuar.Add(new
Class1() { StuName =
"Sachin", StuRollNo = 7, StuAge = 23 });
stuar.Add(new
Class1() { StuName =
"Ravi", StuRollNo = 6, StuAge = 25 });
}
private
void button1_Click(object
sender, EventArgs e)
{
var
res = from sturecords
in stuar.OfType<Class1>()
where (sturecords.StuRollNo == 5)
select (sturecords);
foreach
(Class1 stures in
res)
{
MessageBox.Show("Roll No: " +
stures.StuRollNo + Environment.NewLine +
"Name: " + stures.StuName +
Environment.NewLine +
"Age :" + stures.StuAge);
}
}
private
void button2_Click(object
sender, EventArgs e)
{
var
res = stuar.OfType<Class1>().Where(sturecords
=> sturecords.StuRollNo > 6).Select(sturecords => sturecords);
foreach
(Class1 stures in
res)
{
MessageBox.Show("Roll No: " +
stures.StuRollNo + Environment.NewLine +
"Name: " + stures.StuName +
Environment.NewLine +
"Age :" + stures.StuAge);
}
}
Func<Class1,
bool> whe = delegate(Class1
obj)
{
return
obj.StuRollNo > 5;
};
Func<Class1,
int> ord = delegate(Class1
obj)
{
return
(Int32)obj.StuRollNo;
};
Func<Class1,
Class1> sel =
delegate(Class1 obj)
{
return
obj;
};
private
void button3_Click(object
sender, EventArgs e)
{
var
res = stuar.OfType<Class1>().Where(whe).OrderBy(ord).Select(sel);
foreach
(Class1 stures in
res)
{
MessageBox.Show("Roll No: " +
stures.StuRollNo + Environment.NewLine +
"Name: " + stures.StuName +
Environment.NewLine +
"Age :" + stures.StuAge);
}
}
}
}
Now I am taking about Group by, Count, and Joining with the help of Property
and Linq.
Suppose, You create two different classes , one for "employee" and other is
"department", Lets suppose "employee" class contain "empcode", "empname", "deptcode"
as properties. and department class contain "depname", "depcode", "loc" as
properties.
Then think, if we design same thing in sql as a database table, then how's
structure look like:
Emp Table
EmpCode | EmpName | DeptCode |
1 | Satya | 101 |
2 | Sharad | 101 |
3 | Ravi | 102 |
4 | Mohit | 103 |
5 | Rohit | 104 |
6 | Pramod | 104 |
Dept Table
DeptName | DepCode | Loc |
IT | 101 | New Delhi |
IT | 101 | New Delhi |
Account | 102 | Delhi |
Hr | 103 | Gurgaon |
Clerk | 104 | Noida |
Clerk | 104 | Noida |
And You want to count "total no of department", "recorders by group by" ,
"Joining". with the help of above tables. So in database you follow the joining
concept. Here I do the same thing with the help of Properties and Linq.
Create class For Emp( emp.cs)
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
WindowsFormsApplication2
{
class
emp
{
public
int EmpCode { get;
set; }
public
string EmpName { get;
set; }
public
int DeptName { get;
set; }
}
}
Create a Windows Form and add ref of emp.cs
Ques- Count Total no of dept with group by
Ans
var
res = emps.OfType<emp>().GroupBy(enpsres =>
enpsres.DeptCode);
foreach
(var s in res)
{
MessageBox.Show("Dept Code ="+s.Key.ToString()+"
Total Count= "+s.Count<emp>().ToString());
}
Output4:
Full Code:
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
using
System.Collections;
namespace
WindowsFormsApplication2
{
public
partial class
Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
ArrayList
emps= new ArrayList();
private
void Form1_Load(object
sender, EventArgs e)
{
emps.Add(new
emp() {EmpCode=1, EmpName="Satya",DeptCode=101
});
emps.Add(new
emp() { EmpCode = 2, EmpName =
"Sharad", DeptCode = 101 });
emps.Add(new
emp() { EmpCode = 3, EmpName =
"Ravi", DeptCode = 102 });
emps.Add(new
emp() { EmpCode = 4, EmpName =
"Mohit", DeptCode = 103 });
emps.Add(new
emp() { EmpCode = 5, EmpName =
"Rohit", DeptCode = 104 });
emps.Add(new
emp() { EmpCode = 6, EmpName =
"Pramod", DeptCode = 104 });
}
private
void button1_Click(object
sender, EventArgs e)
{
var
res = emps.OfType<emp>().GroupBy(enpsres =>
enpsres.DeptCode);
foreach
(var s in res)
{
MessageBox.Show("Dept Code ="+s.Key.ToString()+"
Total Count= "+s.Count<emp>().ToString());
}
}
}
}
Create another class For dept( dept.cs)
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
WindowsFormsApplication2
{
class
dept
{
public
string DeptName { get;
set; }
public
int DepCode { get;
set; }
public
string Loc { get;
set; }
}
}
Ques- Findout Empcode, EmpName , DeptName and Loc.
Ans: So, doing all these thing you need the joining because both
properties class are different.
var
res = from empsres in
emps.OfType<emp>()
join deptres in deps.OfType<dept>()
on empsres.DeptCode
equals deptres.DepCode
select (new {
empsres.EmpCode, empsres.EmpName, deptres.DeptName, deptres.Loc });
foreach
(var s in res)
{
MessageBox.Show("EmpCode: "+s.EmpCode+Environment.NewLine+"
EmpName="+s.EmpName+Environment.NewLine+
"DeptName: "+s.DeptName+Environment.NewLine+"Location="+s.Loc
+"");
}
Output5:
Fullcode (Add the ref of "emp.cs" and "dept.cs" in windows form)
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
using
System.Collections;
namespace
WindowsFormsApplication2
{
public
partial class
Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
ArrayList emps= new
ArrayList();
ArrayList deps = new
ArrayList();
private
void Form1_Load(object
sender, EventArgs e)
{
emps.Add(new
emp() {EmpCode=1, EmpName="Satya",DeptCode=101
});
emps.Add(new
emp() { EmpCode = 2, EmpName =
"Sharad", DeptCode = 101 });
emps.Add(new
emp() { EmpCode = 3, EmpName =
"Ravi", DeptCode = 102 });
emps.Add(new
emp() { EmpCode = 4, EmpName =
"Mohit", DeptCode = 103 });
emps.Add(new
emp() { EmpCode = 5, EmpName =
"Rohit", DeptCode = 104 });
emps.Add(new
emp() { EmpCode = 6, EmpName =
"Pramod", DeptCode = 104 });
deps.Add(new
dept() {DeptName="IT",DepCode=101,Loc="New
Delhi" });
deps.Add(new
dept() { DeptName =
"IT", DepCode = 101, Loc =
"New Delhi" });
deps.Add(new
dept() { DeptName =
"Account", DepCode = 102, Loc =
"Delhi" });
deps.Add(new
dept() { DeptName =
"Hr", DepCode = 103, Loc =
"Gurgaon" });
deps.Add(new
dept() { DeptName =
"Clerk", DepCode = 104, Loc =
"Noida" });
deps.Add(new
dept() { DeptName =
"Clerk", DepCode = 104, Loc =
"Noida" });
}
private
void button1_Click(object
sender, EventArgs e)
{
var
res = emps.OfType<emp>().GroupBy(enpsres =>
enpsres.DeptCode);
foreach
(var s in res)
{
MessageBox.Show("Dept Code ="+s.Key.ToString()+"
Total Count= "+s.Count<emp>().ToString());
}
}
private
void button2_Click(object
sender, EventArgs e)
{
var
res = from empsres in
emps.OfType<emp>()
join deptres in deps.OfType<dept>()
on empsres.DeptCode
equals deptres.DepCode
select (new {
empsres.EmpCode, empsres.EmpName, deptres.DeptName, deptres.Loc });
foreach
(var s in res)
{
MessageBox.Show("EmpCode: "+s.EmpCode+Environment.NewLine+"
EmpName="+s.EmpName+Environment.NewLine+
"DeptName: "+s.DeptName+Environment.NewLine+"Location="+s.Loc
+"");
}
}
}
}
In next article I will discuss about Linq and SQL.
No comments:
Post a Comment