Você está na página 1de 9

ICLE's Motilal Jhunjhunwala College of Arts, Commerce &

Science
PRACTICAL NO. 7- Programs using LINQ
1) AIM: Create an array of names (string) and an array of integers separately and perform the following
LINQ queries on them. Further display the query result in a label.
a. Display all the names from the string array.
b. Display all the names from string array which contain l.
c. Display all the numbers from integer array.
d. Display all the numbers incremented by 1 from integer array.
e. Display all the names from names array in upper case and lowercase simultaneously.
CODE:
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using
System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page


{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string[] myarray = { "ASP.NET", "wonderful", "LINQ", "beautiful",
"easy" }; //Get all words using LINQ query
var w = from i in
myarray
select i;
//Print each word of myarray
array Label1.Text = "";
foreach (var word in w)
Label1.Text = Label1.Text + word + " ";
}
protected void Button2_Click(object sender, EventArgs e)
{
string[] myarray = { "ASP.NET", "wonderful", "LINQ", "beautiful",
"easy" }; //Get only those words having "l" using LINQ query
var w = from i in
myarray where
i.Contains('l')
select i;
//print each word containing
"l" Label1.Text = "";
foreach (var word in w)
Label1.Text = Label1.Text + word + " ";
}
protected void Button3_Click(object sender, EventArgs e)
{

ICLE's Motilal Jhunjhunwala College of Arts, Commerce &


Science
int[] myarray = { 97, 98, 99, 81 };
//Get all numbers using LINQ
query var w = from i in myarray
select i;
//Print each number of myarray array
Label1.Text = "";
foreach (var word in w)
Label1.Text = Label1.Text + word + " ";
}
protected void Button4_Click(object sender, EventArgs e)
{
int[] myarray = { 97, 98, 99, 81 };
//Get all numbers using LINQ query and increment them by 1
var w = from i in myarray
select i +1;
//Print each number of myarray array
Label1.Text = "";
foreach (var word in w)
Label1.Text = Label1.Text + word + " ";
}
protected void Button5_Click(object sender, EventArgs e)
{
string[] myarray = { "ASP.NET", "wonderful", "LINQ", "beautiful", "easy" };
//Get all words using LINQ query and change their case
var w = from i in myarray
select i.ToLower() + " " + i.ToUpper();
//Print each word out
Label1.Text = "";
foreach (var word in w)
Label1.Text = Label1.Text + word + " ";
}
}
OUTPUT SCREEN:

ICLE's Motilal Jhunjhunwala College of Arts, Commerce &


Science

2) AIM: Create a XML file with three properties CustNo. And CustName, Salary. Create an application to write
LINQ query to fetch XML file data and display it in gridview.

Steps to add XML file to a website:


Solution Explorer >> right click on website made>>add new item>>XML file>>name it>>add>>write code
CODE:
XMLFile.xml
<?xml version="1.0" encoding="utf8" ?> <Persons>
<Person>
<name>RAJ</name>
<age>24</age>
<address>VASHI</address>
</Person>
<Person>
<name>SAM</name>
<age>34</age>
<address>NERUL</address>
</Person>
<Person>
<name>JOHN</name>
<age>66</age>
<address>VIRAR</address>
</Person>

ICLE's Motilal Jhunjhunwala College of Arts, Commerce &


Science
<Person>
<name>MAHESH</name>
<age>68</age>
<address>ANDHERI</address>
</Person>
</Persons>
Default.aspx.
cs: using
System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
XDocument xmldoc = XDocument.Load(Server.MapPath("XMLFIle.xml"));
var persons = from p in xmldoc.Root.Elements("Person")
where (int.Parse(p.Element("age").Value) < 60)
select new
{
Name = p.Element("name").Value,
Age = p.Element("age").Value,
Address = p.Element("address").Value
};
GridView1.DataSource =
persons;
GridView1.DataBind();
}
}
OUTPUT SCREEN:

ICLE's Motilal Jhunjhunwala College of Arts, Commerce &


Science

3) AIM: Create a class called Customer with two properties CustNo and CustName. Create an application to write
LINQ query to fetch CustName whose CustNo is greater than 5.
CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using System.Collections;
public partial class _Default : System.Web.UI.Page
{
public class Customer
{
public int cid;
public string cname;
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
ArrayList list = new ArrayList();
Customer c1 = new Customer(); c1.cid
= 5;
c1.cname = "Sam";
Customer c2 = new Customer();
c2.cid = 6;
c2.cname = "John";
Customer c3 = new Customer();
c3.cid = 7;
c3.cname = "Tom";
Customer c4 = new Customer();
c4.cid = 8;
c4.cname = "Sara";
list.Add(c1);
list.Add(c2);
list.Add(c3);
list.Add(c4);
var f = from Customer i in list
where i.cid > 5
select i;
foreach (var d in f)

ICLE's Motilal Jhunjhunwala College of Arts, Commerce &


Science
}

Label1.Text += d.cid + " " + d.cname + "<br/>";

OUTPUT SCREEN:

4)

AIM: Write a LINQ Application that displays the data on PageLoad Event and also show how JOINS are used.
CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using
System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using System.Collections;
public partial class _Default : System.Web.UI.Page
{

class Customer
{
public int ID ;
public string Name ;
}
class Order
{
public int ID ;
public string Product ;
}
protected void Page_Load(object sender, EventArgs e)
{
var customers = new Customer[]
{
new Customer{ID = 5, Name = "Sam"},
new Customer{ID = 6, Name = "Dave"},

ICLE's Motilal Jhunjhunwala College of Arts, Commerce &


Science
new Customer{ID = 7, Name =
"Julia"}, new Customer{ID = 8, Name
= "Sue"} };
var orders = new Order[]
{
new Order{ID = 4, Product = "Book"},
new Order{ID = 6, Product = "Game"},
new Order{ID = 7, Product = "Computer"},
new Order{ID = 8, Product = "Shirt"}
};
// Join on the ID properties.
var query = from c in customers
join o in orders on c.ID equals o.ID
select new { c.Name, o.Product };
GridView1.DataSource =
query; GridView1.DataBind();
}
}
OUTPUT SCREEN:

5)

AIM: Create the table with the given fields.


FIELD NAME DATA TYPE
EMPNO number
ENAME
varchar
SAL
number
DNAME
varchar
DEPTNO
number
Design a web page to display the employee information from table to grid control. Use LINQ TO
OBJECTS.
CODE: using
System;
using System.Collections.Generic;
using System.Linq;

ICLE's Motilal Jhunjhunwala College of Arts, Commerce &


Science
using System.Web;
using
System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using System.Collections;
public partial class _Default : System.Web.UI.Page
{
class Customer
{
public int EmpNo;
public string EName;
public double Sal;
public string DName;
public int DeptNo;
}
protected void Page_Load(object sender, EventArgs e)
{
var customers = new Customer[]
{
new Customer{EmpNo = 5, EName = "Sam",Sal=12000,DName="HR",DeptNo=12},
new Customer{EmpNo = 6, EName = "Dave",Sal=14000,DName="Marketing",DeptNo=13},
new Customer{EmpNo = 7, EName = "Julia",Sal=22000,DName="Finance",DeptNo=14},
new Customer{EmpNo = 8, EName = "Sue",Sal=34000,DName="Accounts",DeptNo=15}
};
var query = from c in customers
select new { c.EmpNo, c.EName, c.Sal, c.DName, c.DeptNo };
GridView1.DataSource = query;
GridView1.DataBind();
}
}
OUTPUT SCREEN:

ICLE's Motilal Jhunjhunwala College of Arts, Commerce &


Science

Você também pode gostar