Click here to Skip to main content
15,898,010 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,



I want to fill query results into dataset using Linq
Posted
Comments
Prasad Avunoori 9-May-14 1:08am    
Why do you want to do so?
Member 10226004 9-May-14 1:34am    
because if the request is new it should hit databse and get results from db and it will bind to gridview,If again i request the it should load from the dataset without hiting the db,am saving dataset object in cache

1 solution

C#
 public DataTable GetDepartmentsForSorting(DepartmentInfo _DepartmentInfo)
        {
            QuizEntity entity = new QuizEntity();
            DataTable dt = new DataTable();
            dt.Columns.Add("DeptID", typeof(double));
            dt.Columns.Add("DeptName", typeof(string));
            try
            {
                var a = entity.tblDepartments.AsQueryable();
                if (_DepartmentInfo.DeptName != string.Empty)
                {
                    a = a.Where(e => e.ClientID == _DepartmentInfo.ClientID && e.DeptName.Contains(_DepartmentInfo.DeptName)).OrderByDescending(e => e.DeptID);
                }
                else
                {
                    a = a.Where(e => e.ClientID == _DepartmentInfo.ClientID).OrderBy(e => e.DeptName);
                }
                
                foreach (var dr in a)
                {
                    DataRow row = dt.NewRow();

                    row.SetField<double>("DeptID", dr.DeptID);
                    row.SetField<string>("DeptName", dr.DeptName);

                    dt.Rows.Add(row);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                entity.Dispose();
            }
            return dt;
        }
</string></double>
 
Share this answer
 
Comments
Prasad Avunoori 9-May-14 2:04am    
Nice example Namrata.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900