2011-06-01 43 views

回答

2

我发现这个问题的两个样本;

样品I:

我创建了一个名为LINQToDataTable公共方法如下:

public DataTable LINQToDataTable<T>(IEnumerable<T> varlist) 
{ 
    DataTable dtReturn = new DataTable(); 

    // column names 
    PropertyInfo[] oProps = null; 

    if (varlist == null) return dtReturn; 

    foreach (T rec in varlist) 
    { 
      // 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]; 
        } 

        dtReturn.Columns.Add(new DataColumn(pi.Name, colType)); 
       } 
      } 

      DataRow dr = dtReturn.NewRow(); 

      foreach (PropertyInfo pi in oProps) 
      { 
       dr[pi.Name] = pi.GetValue(rec, null) == null ?DBNull.Value :pi.GetValue 
       (rec,null); 
      } 

      dtReturn.Rows.Add(dr); 
    } 
    return dtReturn; 
} 

样品II

这里是我的第二个方法:

public DataTable ToDataTable(System.Data.Linq.DataContext ctx, object query) 
{ 
    if (query == null) 
    { 
      throw new ArgumentNullException("query"); 
    } 

    IDbCommand cmd = ctx.GetCommand(query as IQueryable); 
    SqlDataAdapter adapter = new SqlDataAdapter(); 
    adapter.SelectCommand = (SqlCommand)cmd; 
    DataTable dt = new DataTable("sd"); 

    try 
    { 
      cmd.Connection.Open(); 
      adapter.FillSchema(dt, SchemaType.Source); 
      adapter.Fill(dt); 
    } 
    finally 
    { 
      cmd.Connection.Close(); 
    } 
    return dt; 
} 
+0

请问如果以TB为单位的数据操作有任何内置函数,e函数是否可行? – usr021986 2011-06-01 06:31:25

+0

兆兆字节?其实我不知道,但我不这么认为。 – 2011-06-01 06:39:37

+0

是否有任何其他解决方案 – usr021986 2011-06-01 07:05:30