Follow Me To get Health and bodybuilding tip's
instagram:sayyedtanveer1410
LINQ stands for Language Integrated Query is a set of features introduced in Visual Studio 2008, using LINQ you can manipulate the data as similar SQL queries .Let us learn it practically how to convert LINQ query result to Datatable by creating one simple function.
Create function to named LINQResultQueryToDataTable which convert LINQ Result To DataTable as
{
- DataTable dt = new DataTable();
- PropertyInfo[] columns = null;
- if (Linqlist == null) return dt;
- foreach (T Record in Linqlist)
- {
- if (columns == null)
- {
- columns = ((Type)Record.GetType()).GetProperties();
- foreach (PropertyInfo GetProperty in columns)
- {
- Type colType = GetProperty.PropertyType;
- if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()
- == typeof(Nullable<>)))
- {
- colType = colType.GetGenericArguments()[0];
- }
- dt.Columns.Add(new DataColumn(GetProperty.Name, colType));
- }
- }
- DataRow dr = dt.NewRow();
- foreach (PropertyInfo pinfo in columns)
- {
- dr[pinfo.Name] = pinfo.GetValue(Record, null) == null ? DBNull.Value : pinfo.GetValue
- (Record, null);
- }
- dt.Rows.Add(dr);
- }
- return dt;
- }
The above function takes the LINQ query result and convert it into the Data Table
No comments:
Post a Comment