Welcome to Sudeep Pandey

Just for sharing ideas and knowledge— My Blog

Linq to Database

Posted by Sudeep Pandey on September 22, 2008

/* This program demonstrate the use of linq in database*/

static void LinqtoDatabase()
{
Console.WriteLine();
profileDataContext pdata = new profileDataContext();
var name = pdata.personals.Where(ss=>ss.firstname.Length>5);
foreach (var nameoutput in name)
{
Console.WriteLine(“firstname: “+nameoutput.firstname);
Console.WriteLine(“middlename: ” + nameoutput.middlename);
Console.WriteLine(“lastname: ” + nameoutput.lastname);
Console.WriteLine(“maritalstatus: ” + nameoutput.maritalstatus);
Console.WriteLine(“age:  ” + nameoutput.age);
Console.WriteLine(“designation : ” + nameoutput.designation);
Console.WriteLine();
}
}

output:

firstname: Sirish
middlename: Man
lastname: Amatya
maritalstatus: Single
age:  27
designation : Senior

firstname: Sudeep
middlename: Raj
lastname: Pandey
maritalstatus: Single
age:  22
designation : Senior

firstname: Deependra
middlename:
lastname: Shrestha
maritalstatus: Single
age:  23
designation : Senior

firstname: Prajjwal
middlename:
lastname: Baral
maritalstatus: Single
age:  27
designation : Senior

firstname: Mukesh
middlename:
lastname: Chapagain
maritalstatus:
age:  23
designation : Senior

Press any key to continue . . .

One Response to “Linq to Database”

  1. sudpan said

    The advantage of using Linq in database is that it is very easy to manipulate the query, which is similar as using in sql or mssql query, and most important features is that no need to write the code for database connection. It will automatically rewrite the code inside the class,so when we call the object of contextdata, it connects with the database.

Leave a Reply

You must be logged in to post a comment.