0
我正在尝试.Include()
模型的属性通过反射,允许我自动包含任何模型类型的所有属性。通过反射包含属性
public static IQueryable<TSource> IncludeAll
<TSource>(this IQueryable<TSource> source)
where TSource : class
{
return typeof(TSource).GetProperties()
.Where(property => property.GetGetMethod().IsVirtual)
.Aggregate(
source,
(current, property) => current.Include(
item => property.GetValue(item, null)));
}
我得到的错误是
InvalidOperationException异常: 属性表达 '项=> __property_0.GetValue(项目,空)' 是无效的。 表达式应表示属性访问:'t => t.MyProperty'。
有没有什么办法可以在lambda中实际引用属性的访问器?