枚举LINQ-to-SQL DataContext

时间:2015-06-25 13:43:49

标签: c# linq linq-to-sql

我希望枚举我使用O / R Designer创建的DataContext中的所有表。

基于this question,我现在正在使用反射这样做,即

PropertyInfo[] dc_properties = data.GetType().GetProperties(); //data is DataContext
PropertyInfo[] row_properties;

foreach (var prop in dc_properties)
{
  object value = prop.GetValue(data);

  if(value is ITable)
  {
    var table = (ITable)value; //value is a table

    foreach (var item in table) //item is a row in the table
    {
      row_properties = item.GetType().GetProperties(); //row_properties values of a row
      foreach (var prop2 in row_properties) 
      {
        object value2 = prop2.GetValue(item); //prop2 a value
      }
    }
  }
}

但是,这很慢。有没有人知道更好的方法来枚举DataContext并获取其中所有表的所有行的值?

谢谢。

0 个答案:

没有答案