Hi Rose.
Refer sample code for your understandability to how to add list object in another list object based.
C#
public partial class CS : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
// Your First data table result currently i am using Test data for result
DataTable dtEmployee = new DataTable();
dtEmployee.Columns.AddRange(new DataColumn[3] { new DataColumn("DepartmentType",typeof(int))
, new DataColumn("title",typeof(string))
, new DataColumn("NoOfEmployee",typeof(int))});
dtEmployee.Rows.Add(1, "Development", 3);
dtEmployee.Rows.Add(2, "Testing", 1);
dtEmployee.Rows.Add(3, "Executive", 2);
// You need any Common column value which will use to make relation from both tables for Title/Department value
// Your Second data table result currently i am using Test data for result
DataTable dtEmployeeDetails = new DataTable();
dtEmployeeDetails.Columns.AddRange(new DataColumn[4] { new DataColumn("DepartmentType",typeof(int))
, new DataColumn("Name",typeof(string))
, new DataColumn("Mobileno",typeof(long))
, new DataColumn("EmailId",typeof(string))});
dtEmployeeDetails.Rows.Add(1, "Ram", 9688542130, "ram@gmail.com");
dtEmployeeDetails.Rows.Add(1, "sam", 9688542160, "sam@gmail.com");
dtEmployeeDetails.Rows.Add(1, "tam", 9688542140, "tam@gmail.com");
dtEmployeeDetails.Rows.Add(2, "john", 9688542160, "john@gmail.com");
dtEmployeeDetails.Rows.Add(3, "jack", 9688542140, "jack@gmail.com");
dtEmployeeDetails.Rows.Add(3, "mark", 9688542140, "mark@gmail.com");
// First list Genrating from class
List<Employee> Employees = new List<Employee>();
for (int i = 0; i < dtEmployee.Rows.Count; i++)
{
List<DataRow> rows = dtEmployeeDetails.Select("DepartmentType=" + (int)dtEmployee.Rows[i]["DepartmentType"]).ToList();
Employee employee = new Employee();
employee.DepartmentType = (int)dtEmployee.Rows[i]["DepartmentType"];
employee.title = (string)dtEmployee.Rows[i]["title"];
employee.NoOfEmployee = (int)dtEmployee.Rows[i]["NoOfEmployee"];
List<EmployeeDetails> employeeDetails1 = new List<EmployeeDetails>();
for (int j = 0; j < rows.Count; j++)
{
EmployeeDetails employeeDetails = new EmployeeDetails
{
DepartmentType = (int)rows[j]["DepartmentType"],
Name = (string)rows[j]["Name"],
Mobileno = (long)rows[j]["Mobileno"],
EmailId = (string)rows[j]["EmailId"]
};
employeeDetails1.Add(employeeDetails);
}
employee.EmployeeDetails = employeeDetails1;
Employees.Add(employee);
}
}
}
}
// Class for For First List result
public class Employee
{
public int DepartmentType { get; set; }
public string title { get; set; }
public int NoOfEmployee { get; set; }
public List<EmployeeDetails> EmployeeDetails { get; set; }
}
// Class for For Second List result
public class EmployeeDetails
{
public int DepartmentType { get; set; }
public string Name { get; set; }
public long Mobileno { get; set; }
public string EmailId { get; set; }
}
VB.Net
Partial Public Class VB
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
' Your First data table result currently i am using Test data for result
Dim dtEmployee As New DataTable()
dtEmployee.Columns.AddRange(New DataColumn(2) {New DataColumn("DepartmentType", GetType(Integer)), New DataColumn("title", GetType(String)), New DataColumn("NoOfEmployee", GetType(Integer))})
dtEmployee.Rows.Add(1, "Development", 3)
dtEmployee.Rows.Add(2, "Testing", 1)
dtEmployee.Rows.Add(3, "Executive", 2)
' You need any Common column value which will use to make relation from both tables for Title/Department value
' Your Second data table result currently i am using Test data for result
Dim dtEmployeeDetails As New DataTable()
dtEmployeeDetails.Columns.AddRange(New DataColumn(3) {New DataColumn("DepartmentType", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Mobileno", GetType(Long)), New DataColumn("EmailId", GetType(String))})
dtEmployeeDetails.Rows.Add(1, "Ram", 9688542130L, "ram@gmail.com")
dtEmployeeDetails.Rows.Add(1, "sam", 9688542160L, "sam@gmail.com")
dtEmployeeDetails.Rows.Add(1, "tam", 9688542140L, "tam@gmail.com")
dtEmployeeDetails.Rows.Add(2, "john", 9688542160L, "john@gmail.com")
dtEmployeeDetails.Rows.Add(3, "jack", 9688542140L, "jack@gmail.com")
dtEmployeeDetails.Rows.Add(3, "mark", 9688542140L, "mark@gmail.com")
' First list Genrating from class
Dim Employees As New List(Of Employee)()
For i As Integer = 0 To dtEmployee.Rows.Count - 1
Dim rows As List(Of DataRow) = dtEmployeeDetails.Select("DepartmentType = " & CInt(dtEmployee.Rows(i)("DepartmentType"))).ToList()
Dim employee As New Employee()
employee.DepartmentType = CInt(dtEmployee.Rows(i)("DepartmentType"))
employee.title = DirectCast(dtEmployee.Rows(i)("title"), String)
employee.NoOfEmployee = CInt(dtEmployee.Rows(i)("NoOfEmployee"))
Dim employeeDetails1 As New List(Of EmployeeDetails)()
For j As Integer = 0 To rows.Count - 1
Dim employeeDetails As New EmployeeDetails() With { _
.DepartmentType = CInt(rows(j)("DepartmentType")), _
.Name = DirectCast(rows(j)("Name"), String), _
.Mobileno = CLng(rows(j)("Mobileno")), _
.EmailId = DirectCast(rows(j)("EmailId"), String) _
}
employeeDetails1.Add(employeeDetails)
Next
employee.EmployeeDetails = employeeDetails1
Employees.Add(employee)
Next
End If
End Sub
End Class
' Class for For First List result
Public Class Employee
Public Property DepartmentType() As Integer
Get
Return m_DepartmentType
End Get
Set(value As Integer)
m_DepartmentType = value
End Set
End Property
Private m_DepartmentType As Integer
Public Property title() As String
Get
Return m_title
End Get
Set(value As String)
m_title = value
End Set
End Property
Private m_title As String
Public Property NoOfEmployee() As Integer
Get
Return m_NoOfEmployee
End Get
Set(value As Integer)
m_NoOfEmployee = value
End Set
End Property
Private m_NoOfEmployee As Integer
Public Property EmployeeDetails() As List(Of EmployeeDetails)
Get
Return m_EmployeeDetails
End Get
Set(value As List(Of EmployeeDetails))
m_EmployeeDetails = value
End Set
End Property
Private m_EmployeeDetails As List(Of EmployeeDetails)
End Class
' Class for For Second List result
Public Class EmployeeDetails
Public Property DepartmentType() As Integer
Get
Return m_DepartmentType
End Get
Set(value As Integer)
m_DepartmentType = value
End Set
End Property
Private m_DepartmentType As Integer
Public Property Name() As String
Get
Return m_Name
End Get
Set(value As String)
m_Name = value
End Set
End Property
Private m_Name As String
Public Property Mobileno() As Long
Get
Return m_Mobileno
End Get
Set(value As Long)
m_Mobileno = value
End Set
End Property
Private m_Mobileno As Long
Public Property EmailId() As String
Get
Return m_EmailId
End Get
Set(value As String)
m_EmailId = value
End Set
End Property
Private m_EmailId As String
End Class
Screenshot
