Welcome to Sudeep Pandey

Just for sharing ideas and knowledge— My Blog

Archive for the ‘C#’ Category

Linq to Xml

Posted by Sudeep Pandey on September 22, 2008

Xml file: student.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<students>
<student>
<name>sudeep</name>
<roll>982102</roll>
<countries>
<country id=”1″>nepal</country>
<country id=”2″>usa</country>
</countries>
</student>
<student>
<name>abhinab</name>
<roll>56780</roll>
<countries>
<country id=”1″>uk</country>
<country id=”2″>japan</country>
</countries>
</student>
<student>
<name>suraj</name>
<roll>43223</roll>
<countries>
<country id=”1″>uae</country>
<country id=”2″>australia</country>
</countries>
</student>
</students>

C# method to extract name

static void LinqtoXml()
{

XElement doc = XElement.Load(“student.xml”);
var name=from stname in doc.Descendants(“name”) where stname.Value.StartsWith(“s”) select stname.Value;
foreach (var sname in name)
{
Console.WriteLine(“name: {0}”, sname);
}
}

output:

name:sudeep

name:suraj

Posted in LINQ | Leave a Comment »

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 . . .

Posted in LINQ | 1 Comment »

Linq to Collection

Posted by Sudeep Pandey on September 22, 2008

/*This method display the name whose length is greater than 5*/

static void LinqtoCollection()
{
string[] str ={“Sudeep”,
“Ranjeet”,
“Umesh”,
“Raju”,
“Rajan”,
“Heera”,
“Shyam”,
“Deepesh”,
“Manoj”
};
IEnumerable<string> stroutput = from name in str where name.Length > 5 select name;
foreach (string s in stroutput)
{
Console.WriteLine(s);
}

Output:

Sudeep

Ranjeet

Deepesh

Posted in LINQ | Leave a Comment »