LINQ To DataSet

Here, I would like to describe how to use a LINQ query and describe how to query a dataset data.

I will describe the following listed points. I have described all points using a simple example.

Restriction Operator

Where: It's a Restriction operator, it selects a value from a collection. In my example my data source is a dataset. I need to describe both the lambda expression and a simple query. My Data Source is on the following written data I need to query.

/// <summary>

/// Here my Data
/// </summary>

/// <returns></returns>

private static DataSet MyData()

{

    DataSet _ds = new DataSet();

    DataTable _dt = new DataTable();

    _dt.Columns.Add("Employeeid");

    _dt.Columns.Add("Name");

    _dt.Columns.Add("Age");

    _dt.Columns.Add("Qualification");

    _dt.Columns.Add("FatherName");

    _dt.Columns.Add("Address");

    _dt.Columns.Add("Gender");//Here 1 for Female and 2 for Male.

    _dt.Rows.Add("1001", "Priya", "24", "B-Tech", "Suresh Yadav", "Delhi", "1");

    _dt.Rows.Add("1002", "priyanka", "26", "B-Tech", "Sudhir singh", "Patna", "1");

    _dt.Rows.Add("1003", "Anu", "25", "MCA", "Sekhar singh", "bhagalpur", "1");

    _dt.Rows.Add("1004", "Suman", "26", "BCA", "Rohit singh", "Indor", "1");

    _dt.Rows.Add("1005", "Vijay Mishra", "26", "B-tech", "Raju Mishra", "Patna", "2");

    _dt.Rows.Add("1006", "Rekha", "24", "B-tech", "Sudhakar singh", "Chandigarh", "1");

    _dt.Rows.Add("1007", "Ankush kumar", "28", "B-tech", "Sunil Kumar", "Delhi", "2");

    _ds.Tables.Add(_dt);

    return _ds;

}

1. Here the simple query is like the SQL query example (Select * from Tablename where colname='jdsfdj):

 //(1) Simple query :Like Sql query =>Select * from Tablename where colname='jdsfdj'

Console.WriteLine("---------------Simple Query to=> select all column data---------------");

Console.WriteLine("Enter employee id");

string ss=Console.ReadLine();

var data = from s in MyData().Tables[0].AsEnumerable()

        where s["Employeeid"].ToString()==ss

        select s;

foreach (var mydata in data)

{

      Console.WriteLine("EmployeeID:" + mydata["Employeeid"] + "," + "Name:" + mydata["Name"] + ",Gender:" +( mydata["Gender"].ToString() == "1" ? "Female" : mydata["Gender"].ToString() == "2" ? "Male" : "") + ",Age:" + mydata["Age"] +

       ",Qualification:" + mydata["Qualification"] + ",FatherName:" + mydata["FatherName"] + ",Address:" + mydata["Address"] + Environment.NewLine);

}

The output of the above written query is:

SQL query

2. Here I have select all data using a lambda expression.
 

//(2)Simple query  =>using linq Method=>Lambda Expression:Like Sql query =>Select * from Tablename

Console.WriteLine("-----Simple Query to=>select all column data using Lambda Expression of linq-------");

Console.WriteLine("Enter employee id");

string empid = Console.ReadLine();

var data2 = MyData().Tables[0].AsEnumerable().Select(k => k).Where(p => p["Employeeid"].ToString() == empid);

foreach (var mydata in data2)

{

     Console.WriteLine("EmployeeID:" + mydata["Employeeid"] + "," + "Name:" + mydata["Name"] + ",Gender:" + (mydata["Gender"].ToString() == "1" ? "Female" : mydata["Gender"].ToString() == "2" ? "Male" : "") + ",Age:" + mydata["Age"] +

    ",Qualification:" + mydata["Qualification"] + ",FatherName:" + mydata["FatherName"] + ",Address:" + mydata["Address"] + Environment.NewLine);

}

The output of the above written query is:

Output 


Similar Articles