开发者

Iterating tables in a context and the properties of those tables

开发者 https://www.devze.com 2023-01-10 05:04 出处:网络
I\'m iterating the tables of a context and then the properties of those tables to eager load all columns in a context. I received some help via another question, but I don\'t seem to be able to figure

I'm iterating the tables of a context and then the properties of those tables to eager load all columns in a context. I received some help via another question, but I don't seem to be able to figure out how to iterate the column properties of the actual table.


Final working code:

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

    var contextTables = context.GetType().GetPr开发者_如何学Gooperties().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;
}


You're only getting the ProperInfos. You need to get the values from the PropertyInfos:

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

}

Once you have the PropertyInfo class, you need to get the value using the GetValue method.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号