using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
class IndexName
{
public int Index { get; set; }
public string Name { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Index"));
dt.Columns.Add(new DataColumn("Name"));
for (int i = 0; i < 10; i++) { DataRow row = dt.NewRow(); row["Index"] = i; row["Name"] = "dummyData" + i.ToString(); dt.Rows.Add(row); }
//bind the gridview
List<string> obj = new List<string>();
obj = dt.Select().Select(t => t.Field<string>("index")).ToList();
List<IndexName> obj2 = new List<IndexName>();
obj2 = (from DataRow dr in dt.Rows
select new IndexName
{
Index = Convert.ToInt32(dr["Index"]),
Name = Convert.ToString(dr["Name"])
}).ToList();
}
}
Second Example:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using employee;
using System.Collections.Generic;
public partial class Default2 : System.Web.UI.Page
{
DataSet ds = new DataSet("Collections");
DataTable dt = new DataTable();
DataTable dt1 = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
TestCollections();
//FillData();
LINQJoinOnDataTable();
}
private void TestCollections()
{
dt.Columns.Add("SrNo", Type.GetType("System.Int32"));
dt.Columns.Add("Data", Type.GetType("System.String"));
DataRow dr = dt.NewRow();
dr["SrNo"] = "1";
dr["Data"] = "A";
dt.Rows.Add(dr);
DataRow dr1 = dt.NewRow();
dr1["SrNo"] = "2";
dr1["Data"] = "B";
dt.Rows.Add(dr1);
dt1.Columns.Add("SrNo", Type.GetType("System.Int32"));
dt1.Columns.Add("Data", Type.GetType("System.String"));
DataRow dr2 = dt1.NewRow();
dr2["SrNo"] = "1";
dr2["Data"] = "One";
dt1.Rows.Add(dr2);
DataRow dr3 = dt1.NewRow();
dr3["SrNo"] = "1";
dr3["Data"] = "Two";
dt1.Rows.Add(dr3);
DataRow dr4 = dt1.NewRow();
dr4["SrNo"] = "3";
dr4["Data"] = "Three";
dt1.Rows.Add(dr4);
//var data = dt.Select().Select(t => new { SrNo = Convert.ToInt32(t["SrNo"]), Data = Convert.ToString(t["Data"]) }).Where(t => t.Data == "B").ToList();
//int abc = (from d in data select d.SrNo).SingleOrDefault();
//var data1 = dt1.Select().Select(t => new { SrNo = Convert.ToInt32(t["SrNo"]), Data = Convert.ToString(t["Data"]) }).Where(t => t.SrNo == abc).ToList();
//string[] a = (from d in data1 select d.Data).ToArray();
}
private void FillData()
{
Employee emp1 = new Employee() { ID = 1, Name = "A", DeptID = 1 };
Employee emp2 = new Employee() { ID = 2, Name = "B", DeptID = 1 };
Employee emp3 = new Employee() { ID = 3, Name = "C", DeptID = 1 };
Employee emp4 = new Employee() { ID = 4, Name = "D", DeptID = 2 };
Employee emp5 = new Employee() { ID = 5, Name = "E", DeptID = 2 };
Employee emp6 = new Employee() { ID = 6, Name = "F", DeptID = 2 };
Employee emp7 = new Employee() { ID = 7, Name = "G", DeptID = 6 };
Employee emp8 = new Employee() { ID = 8, Name = "H", DeptID = 3 };
Employee emp9 = new Employee() { ID = 9, Name = "I", DeptID = 3 };
Employee emp10 = new Employee() { ID = 10, Name = "J", DeptID = 7 };
Employee emp11 = new Employee() { ID = 11, Name = "K", DeptID = 7 };
Employee emp12 = new Employee() { ID = 12, Name = "L", DeptID = 5 };
Department Dept1 = new Department() { ID = 1, Name = "Development" };
Department Dept2 = new Department() { ID = 2, Name = "Testing" };
Department Dept3 = new Department() { ID = 3, Name = "Marketing" };
Department Dept4 = new Department() { ID = 4, Name = "Support" };
List<Employee> ListOfEmployees = new List<Employee>();
ListOfEmployees.AddRange((new Employee[] { emp1, emp2, emp3, emp4, emp5, emp6, emp7,emp8, emp9, emp10, emp11, emp12 }));
List<Department> ListOfDepartment = new List<Department>();
ListOfDepartment.AddRange(new Department[] { Dept1, Dept2, Dept3, Dept4 });
var data = from emp in ListOfEmployees
join dept in ListOfDepartment on emp.DeptID equals dept.ID where emp.DeptID==1
select new
{
EmployeeName = emp.Name,
DepartmentName=dept.Name
};
}
private void LINQJoinOnDataTable()
{
var result = from p in dt.AsEnumerable()
join q in dt1.AsEnumerable() on p.Field<int>("SrNo") equals q.Field<int>("SrNo") where p.Field<string>("Data")=="A"
select new
{
ID = q.Field<int>("SrNo"),
Type = q.Field<string>("Data")
};
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
class IndexName
{
public int Index { get; set; }
public string Name { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Index"));
dt.Columns.Add(new DataColumn("Name"));
for (int i = 0; i < 10; i++) { DataRow row = dt.NewRow(); row["Index"] = i; row["Name"] = "dummyData" + i.ToString(); dt.Rows.Add(row); }
//bind the gridview
List<string> obj = new List<string>();
obj = dt.Select().Select(t => t.Field<string>("index")).ToList();
List<IndexName> obj2 = new List<IndexName>();
obj2 = (from DataRow dr in dt.Rows
select new IndexName
{
Index = Convert.ToInt32(dr["Index"]),
Name = Convert.ToString(dr["Name"])
}).ToList();
}
}
Second Example:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using employee;
using System.Collections.Generic;
public partial class Default2 : System.Web.UI.Page
{
DataSet ds = new DataSet("Collections");
DataTable dt = new DataTable();
DataTable dt1 = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
TestCollections();
//FillData();
LINQJoinOnDataTable();
}
private void TestCollections()
{
dt.Columns.Add("SrNo", Type.GetType("System.Int32"));
dt.Columns.Add("Data", Type.GetType("System.String"));
DataRow dr = dt.NewRow();
dr["SrNo"] = "1";
dr["Data"] = "A";
dt.Rows.Add(dr);
DataRow dr1 = dt.NewRow();
dr1["SrNo"] = "2";
dr1["Data"] = "B";
dt.Rows.Add(dr1);
dt1.Columns.Add("SrNo", Type.GetType("System.Int32"));
dt1.Columns.Add("Data", Type.GetType("System.String"));
DataRow dr2 = dt1.NewRow();
dr2["SrNo"] = "1";
dr2["Data"] = "One";
dt1.Rows.Add(dr2);
DataRow dr3 = dt1.NewRow();
dr3["SrNo"] = "1";
dr3["Data"] = "Two";
dt1.Rows.Add(dr3);
DataRow dr4 = dt1.NewRow();
dr4["SrNo"] = "3";
dr4["Data"] = "Three";
dt1.Rows.Add(dr4);
//var data = dt.Select().Select(t => new { SrNo = Convert.ToInt32(t["SrNo"]), Data = Convert.ToString(t["Data"]) }).Where(t => t.Data == "B").ToList();
//int abc = (from d in data select d.SrNo).SingleOrDefault();
//var data1 = dt1.Select().Select(t => new { SrNo = Convert.ToInt32(t["SrNo"]), Data = Convert.ToString(t["Data"]) }).Where(t => t.SrNo == abc).ToList();
//string[] a = (from d in data1 select d.Data).ToArray();
}
private void FillData()
{
Employee emp1 = new Employee() { ID = 1, Name = "A", DeptID = 1 };
Employee emp2 = new Employee() { ID = 2, Name = "B", DeptID = 1 };
Employee emp3 = new Employee() { ID = 3, Name = "C", DeptID = 1 };
Employee emp4 = new Employee() { ID = 4, Name = "D", DeptID = 2 };
Employee emp5 = new Employee() { ID = 5, Name = "E", DeptID = 2 };
Employee emp6 = new Employee() { ID = 6, Name = "F", DeptID = 2 };
Employee emp7 = new Employee() { ID = 7, Name = "G", DeptID = 6 };
Employee emp8 = new Employee() { ID = 8, Name = "H", DeptID = 3 };
Employee emp9 = new Employee() { ID = 9, Name = "I", DeptID = 3 };
Employee emp10 = new Employee() { ID = 10, Name = "J", DeptID = 7 };
Employee emp11 = new Employee() { ID = 11, Name = "K", DeptID = 7 };
Employee emp12 = new Employee() { ID = 12, Name = "L", DeptID = 5 };
Department Dept1 = new Department() { ID = 1, Name = "Development" };
Department Dept2 = new Department() { ID = 2, Name = "Testing" };
Department Dept3 = new Department() { ID = 3, Name = "Marketing" };
Department Dept4 = new Department() { ID = 4, Name = "Support" };
List<Employee> ListOfEmployees = new List<Employee>();
ListOfEmployees.AddRange((new Employee[] { emp1, emp2, emp3, emp4, emp5, emp6, emp7,emp8, emp9, emp10, emp11, emp12 }));
List<Department> ListOfDepartment = new List<Department>();
ListOfDepartment.AddRange(new Department[] { Dept1, Dept2, Dept3, Dept4 });
var data = from emp in ListOfEmployees
join dept in ListOfDepartment on emp.DeptID equals dept.ID where emp.DeptID==1
select new
{
EmployeeName = emp.Name,
DepartmentName=dept.Name
};
}
private void LINQJoinOnDataTable()
{
var result = from p in dt.AsEnumerable()
join q in dt1.AsEnumerable() on p.Field<int>("SrNo") equals q.Field<int>("SrNo") where p.Field<string>("Data")=="A"
select new
{
ID = q.Field<int>("SrNo"),
Type = q.Field<string>("Data")
};
}
}
No comments:
Post a Comment