In this article, I will explain you how to Convert Isingleresult to List and DataTable using C#.
Related Articles
- What is LINQ to SQL and how to create LINQ to SQL class in C# with example for beginners
- LINQ to SQL: GridView select, insert, update and delete using C# with asp.net
- LINQ to SQL: Calling stored procedure with select, insert, update and delete in asp.net
- Insert, update, delete, crud operation in asp.net gridview using stored procedure
Converting ISingleResult to List
Below is the code to which will Convert Isingleresult to List type in C#.
EmployeeDataContext employeeDbContext = new EmployeeDataContext();
ISingleResultusp_EmployeeResult> result = employeeDbContext.usp_Employee();
System.Collections.Generic.ListEmployee> emp = new ListEmployee>();
foreach (usp_EmployeeResult employee in result)
{
emp.Add( new Employee { Id=employee.Id,Name= employee.Name,Gender= employee.Gender,Salary=employee.Salary});
}
if (emp.Count > 0)
{
grdEmployee.DataSource = emp;
grdEmployee.DataBind();
}
Converting ISingleResult to DataTable
Below is the code to which will convert ISingleResult to DataTable in C#.
EmployeeDataContext employeeDbContext = new EmployeeDataContext();
ISingleResultusp_EmployeeResult> result = employeeDbContext.usp_Employee();
DataTable emp = new DataTable();
emp.Columns.Add("Id", typeof(Int32));
emp.Columns.Add("Name", typeof(string));
emp.Columns.Add("Gender", typeof(string));
emp.Columns.Add("Salary", typeof(Int32));
foreach (usp_EmployeeResult employee in result)
{
emp.Rows.Add(employee.Id, employee.Name, employee.Gender, employee.Salary);
}
if (emp.Rows.Count > 0)
{
grdEmployee.DataSource = emp;
grdEmployee.DataBind();
}