Linq 在上下文中迭代表以及这些表的属性

Linq 在上下文中迭代表以及这些表的属性,linq,linq-to-sql,datacontext,Linq,Linq To Sql,Datacontext,我正在迭代上下文中的表,然后遍历这些表的属性以加载上下文中的所有列。我通过另一个问题得到了一些帮助,但我似乎无法理解如何迭代实际表的列属性 最终工作代码: public static void DisableLazyLoading(this DataContext context) { DataLoadOptions options = new DataLoadOptions(); var contextTables = context.GetType().GetProper

我正在迭代上下文中的表,然后遍历这些表的属性以加载上下文中的所有列。我通过另一个问题得到了一些帮助,但我似乎无法理解如何迭代实际表的列属性


最终工作代码:

public static void DisableLazyLoading(this DataContext context)
{
    DataLoadOptions options = new DataLoadOptions();

    var contextTables = context.GetType().GetProperties().Where(n => n.PropertyType.Name == "Table`1");
    foreach (var contextTable in contextTables)
    {
        var tableType = contextTable.GetValue(context, null).GetType().GetGenericArguments()[0];
        var tableProperties = tableType.GetProperties().Where(n => n.PropertyType.Name != "EntitySet`1");
        foreach (var tableProperty in tableProperties)
        {
            ParameterExpression paramExp = Expression.Parameter(tableType, "s");
            Expression expr = Expression.Property(paramExp, tableProperty.Name);
            options.LoadWith(Expression.Lambda(expr, paramExp));
        }
    }

    context.LoadOptions = options;
}

您只获得了
ProperInfo
s。您需要从
PropertyInfo
s获取值

var tablePropertInfos = context.GetType().GetProperties().Where(
    n => n.PropertyType.Name == "Table`1");

foreach (var tablePropertyInfo in tablePropertInfos)
{

    // Get the actual table
    var table = tablePropertyInfo.GetValue(context, null);

    // Do the same for the actual table properties

}

拥有
PropertyInfo
类后,需要使用
GetValue
方法获取值。

为什么它会获取
PropertyInfo
类的属性。我不知道如何得到实际的表。啊,
GetValue
现在对它的作用有了很大的意义。这非常接近,因为它为每个类型返回一个
1`type。您知道如何将其转换为定义表列的实际类/类型吗?现在,我只得到
1`的属性,而不是
订单
订单项
表的属性。例如,用当前代码更新了问题。我只是在调试内部
foreach
时才得到这两个:那么您想要
表的泛型类型的属性?例如,在表中,您希望
GetProperties
返回
Foo
的属性?多亏了。谢谢你的帮助!没问题!我自己也在寻找答案:)。