CSharp lambda in detail2

C# lambda expressions in detail part 2
lambda expression
image credit:http://www.daedtech.com

In our CSharp lambda expressions in detail part 2 post, we will use the power of Lambda Expressions combined with LINQ(Language Integrated Query).
You might be thinking about this way of programming is not possible.The best thing in this world is :
“Seeing and believing” .
First, lets discuss why we are using Lambda with SQL because SQL is already an excellent way of writing queries?

Pros of LINQ:

Type safety is assured.
Multi-threading with LINQ works great.
LINQ syntax is quite developer’s friendly and addictive
LINQ is debugged by .Net Framework.
LINQ can simultaneously work on more than two databases

Cons of LINQ:

Writing complex queries can make the application work very slow in large databases
Changes in query impact on the assemblies to be recompiled

Now, its time to write some code, but first, we have to explain about the dataset and linq to dataset

A dataset is a collection of data.

In simple language, a dataset is a memory representation of actual tables.

A typed DataSet is a class that derives from a DataSet. As such, it inherits all the methods, events, and properties of a DataSet. Additionally, a typed DataSet provides strongly typed methods, events, and properties. This means you can access tables and columns by name, instead of using collection-based methods. Aside from the improved readability of the code, a typed DataSet also allows the Visual Studio .NET code editor to automatically complete lines as you type.

reference:http://msdn.microsoft.com/en-us/library/esbykkzb(v=vs.110).aspx

/* myQuery variable from the datatable casted as Enumerable to get the resultset in Enumerable type
because without typecasting, we can’t get the results from the dataset*/
var myQuery = from employee in myDataTable.AsEnumerable()
//get the employee with the name Stephen
where (string)employee[“employeeName”] ==”Stephen”
select employee;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{

DataSet myDataSet = new DataSet();

DataTable employees = myDataSet.Tables[“Employees”];

var myQuery = employees.AsEnumerable().
Select(employee => new
{
employeeName = employee.Field(“employeeName”),
employeeAge = employee.Field(“employeeAge”),
employeeDesignation = employee.Field(“employeeDesignation”)
});

Console.WriteLine(“Employee Info:”);
foreach (var employeeInfo in myQuery)
{
Console.WriteLine(“employeeName: {0} employeeAge: {1} employeeDesignation: {2} “,
employeeInfo.employeeName, employeeInfo.employeeAge, employeeInfo.employeeDesignation);
}

}
}
}

Also read CSharp Lambda in Detail 3

Syed Adeel Ahmed
Analyst, Programmer, Educationist and Blogger at Technofranchise
Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.
Syed Adeel Ahmed on FacebookSyed Adeel Ahmed on GoogleSyed Adeel Ahmed on InstagramSyed Adeel Ahmed on Twitter

Published by

Syed Adeel Ahmed

Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.