4 August 2013

Datatable to List in C# using Linq

Datatable to List in C# using Linq

//StudentDetails Class with Entity Details
 public class StudentDetails
    {
        public int StudentID { get; set; }
        public string StudentName { get; set; }
        public string ClassName { get; set; }
        public string Section { get; set; }
    }
  

//Dataaccess part, Microsoft Sqlhelper class  used
 public List GetStudentDetails(int studentID)
        {
            DataSet ds = new DataSet();
            SqlParameter[] SqlParam = null;
            SqlParam = new SqlParameter[1];
            SqlCommand SqlCmd = new SqlCommand();
            List objStud = new List();
            try
            {
                SqlParam[0] = new SqlParameter("@StudentID", SqlDbType.Int);
                SqlParam[0].Value = studentID;
                SqlHelper.FillDataset(DBconnection(), CommandType.StoredProcedure, "usp_Get_StudentDetails", ds, new string[] { "Display" }, SqlParam);
               
 objStud = ds.Tables[0].AsEnumerable().Select(data => new StudentDetails() { StudentID = (int)data["StudentID"], StudentName = (string)data["StudentName"], ClassName = (string)data["ClassName"], Section = (string)data["Section"] }).ToList();
                return objStud;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (ds != null)
                {
                    ds.Dispose();
                }
                objStud = null;
            }
        }


No comments:

Post a Comment

Comments Welcome

Implementing OAuth validation in a Web API

 I mplementing OAuth validation in a Web API Implementing OAuth validation in a Web API using C# typically involves several key steps to sec...