Here I would like to describe how to use a LINQ query and describe how to query dataset data.
I will describe the following points. I have described all points using a simple example.
(A) Select: It's a Projection operator, it selects a value from a collection.
In my example my data source is a dataset. I need to describe both a lambda expression and a simple query.
1. Here a simple query is like SQL query Example(Select * from tablename):
(1) var data = from s in MyData().Tables[0].AsEnumerable()
(2) select s;
(3) foreach (var mydata in data)
(4) {
(5) Console.WriteLine("EmployeeID:" + mydata["Employeeid"] + "," + "Name:" + mydata["Name"] +
"Gender:" + mydata["Gender"] + ",Age:" +
mydata["Age"] +
(6) ",Qualification:" + mydata["Qualification"] + ",FatherName:" + mydata["FatherName"] + ",Address:"
+ mydata["Address"] + Environment.NewLine);
2. Here I have selected all data using a lambda expression.
Console.WriteLine("-----------Simple Query to=>select all column data using Lambda Expression of linq----------------");
var data2 = MyData().Tables[0].AsEnumerable().Select(k => k);
foreach (var mydata in data2)
{
Console.WriteLine("EmployeeID:" + mydata["Employeeid"] + "," + "Name:" + mydata["Name"] + "Gender:"
+ mydata["Gender"] + ",Age:" + mydata["Age"] +
",Qualification:" + mydata["Qualification"] + ",FatherName:" + mydata["FatherName"] + ",Address:" +
mydata["Address"] + Environment.NewLine);
}
3. Here select a specific column like SQL query (select column1,column2 from tablename):
//Like Sql query =>select columnname1,columnname2 from tablename
Console.WriteLine("--------------------------Simple Query to=> select specific column data--
------------------
--------");
var data1 = from p in MyData().Tables[0].AsEnumerable()
select new { Employeeid = p["Employeeid"].ToString(), Name = p["Name"].ToString() };
foreach (var mydata1 in data1)
{
Console.WriteLine(mydata1.Employeeid + "," + mydata1.Name);
}
4. Here I need to convert the query above into a lambda expression.
//Using Lambda expression=>Like Sql query =>select columnname1,columnname2 from tablename
Console.WriteLine("-----------Simple Query to=>select all column data using Lambda Expression of linq---
-------------");
var data3 = MyData().Tables[0].AsEnumerable().Select(k => new { Employeeid = k["Employeeid"], Name
= k["Name"] });
foreach (var mydata1 in data3)
{
Console.WriteLine(mydata1.Employeeid + "," + mydata1.Name);
}
5. Here I need to show how to convert a SQL Case statement inro a LINQ.
//Using Lambda expression=>Like Sql query=>use case in Sql query
//=>select columnname1,columnname2,case when columnname3 = 1 then 'Female' when columnname3
= 2 then 'Male' from tablename
Console.WriteLine("-----------Simple Query to=>select all column data using Lambda Expression of linq----------------");
var data4 = MyData().Tables[0].AsEnumerable().Select(k => new
{
Employeeid = k["Employeeid"],
Name = k["Name"],
Gender = k["Gender"].ToString() == "1" ? "Female" : k["Gender"].ToString() == "2" ? "Male" : ""
});
foreach (var mydata1 in data4)
{
Console.WriteLine(mydata1.Employeeid + "," + mydata1.Name + "," + mydata1.Gender);
In the next article I need to describe the Restriction Operator.