Sunday, 18 December 2016

Convert ISingleResult to List or DataTable in C#

In this article, I will explain you how to convert ISingleResult to List and DataTable using C#.

Related Articles

  1. What is LINQ to SQL and how to create LINQ to SQL class in C# with example for beginners
  2. LINQ to SQL: GridView select, insert, update and delete using C# with asp.net
  3. LINQ to SQL: Calling stored procedure with select, insert, update and delete in asp.net
  4. 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();
ISingleResult<usp_EmployeeResult> result = employeeDbContext.usp_Employee();
 
System.Collections.Generic.List<Employee> emp = new List<Employee>();
 
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();
ISingleResult<usp_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();
}
Share:

0 comments:

Post a Comment

Email Subscription

Subscribe to our newsletter to get the latest articles directly into your inbox

Blog Archive

BUY FROM AMAZON