Linq to Dataset Example:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LinqDatasetEX2.aspx.cs" Inherits="LinqDatasetEX2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="Gv_Data" runat="server"> </asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class LinqDatasetEX2 : System.Web.UI.Page
{
LinqDataClassesDataContext dc = new LinqDataClassesDataContext();
protected void Page_Load(object sender, EventArgs e)
{
string sql = "Select * From Emp";
SqlDataAdapter da = new SqlDataAdapter(sql, dc.Connection.ConnectionString);
DataTable dt = new DataTable();
da.Fill(dt);
string sql1 = "Select * From Dept";
SqlDataAdapter da1 = new SqlDataAdapter(sql1, dc.Connection.ConnectionString);
DataTable dt1 = new DataTable();
da1.Fill(dt1);
string sql2 = "Select * From Desig";
SqlDataAdapter da2 = new SqlDataAdapter(sql2, dc.Connection.ConnectionString);
DataTable dt2 = new DataTable();
da2.Fill(dt2);
//Convert DataTable To Linq
var query = from emp in dt.AsEnumerable()
join dept in dt1.AsEnumerable() on emp.Field<int>("Dept_Id") equals
dept.Field<int>("DeptId")
join desig in dt2.AsEnumerable() on emp.Field<int>("Desig_Id") equals
desig.Field<int>("DesigId")
orderby emp.Field<string>("EmpName")
select new
{
EmpName = emp.Field<string>("EmpName"),
EmpPhone = emp.Field<int>("EmpPhone"),
Photo = emp.Field<string>("Loc"),
DeptName = dept.Field<string>("DeptName"),
DesigName = desig.Field<string>("DesigName"),
Salary = emp.Field<int>("Salary")
};
DataTable dt5 = new DataTable();
dt5.Columns.Add("EmpName");
dt5.Columns.Add("EmpPhone");
dt5.Columns.Add("Loc");
dt5.Columns.Add("DeptName");
dt5.Columns.Add("DesigName");
dt5.Columns.Add("Salary");
//Convert Linq to DataTable
foreach (var element in query)
{
DataRow row = dt5.NewRow();
row["EmpName"] = element.EmpName;
row["EmpPhone"] = element.EmpPhone;
row["Photo"] = element.Loc;
row["DeptName"] = element.DeptName;
row["DesigName"] = element.DesigName;
row["Salary"] = element.Salary;
dt5.Rows.Add(row);
}
Gv_Data.DataSource = dt5;
Gv_Data.DataBind();
}
}
No comments:
Post a Comment