- C# code
#region Linq to datatable. /// <summary> /// Linq to datatable. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="data"></param> /// <returns></returns> public static DataTable LinqToDataTable<T>(IEnumerable<T> data) { var dt = new DataTable(); // column names PropertyInfo[] oProps = null; // Could add a check to verify that there is an element 0 foreach (T rec in data) { // Use reflection to get property names, to create table, Only first time, others will follow if (oProps == null) { oProps = ((Type)rec.GetType()).GetProperties(); foreach (PropertyInfo pi in oProps) { Type colType = pi.PropertyType; if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>))) { colType = colType.GetGenericArguments()[0]; } dt.Columns.Add(new DataColumn(pi.Name, colType)); } } DataRow dr = dt.NewRow(); foreach (PropertyInfo pi in oProps) { dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue(rec, null); } dt.Rows.Add(dr); } return dt; } #endregion
- C# code
#region 创建临时表 var _filter = from q in mdc.WebSuite_Catalogs where q.CatalogCancellation == false orderby q.CatalogOrder select new { q.CatalogId, q.CatalogRootId, q.CatalogOrder, q.CatalogName, q.CatalogCode, q.NavigationLicence, q.CatalogPath }; DataTable dt = LinqToDataTable(_filter); #endregion
- C# code
public ActionResult CreateCategory(string Name, string status, string kind) { //我该在这里如何写代码,去调用我的临时表?? return RedirectToAction("Index"); } #endregion
------解决方案--------------------------------------------------------
DataTable dt = LinqToDataTable(_filter);
调用这个dt?