C# IQueryable<;T>;OrderBy<;T>;扩展失败,带有外键属性
这是MyPageViewModel:C# IQueryable<;T>;OrderBy<;T>;扩展失败,带有外键属性,c#,asp.net-mvc,entity-framework,sorting,iqueryable,C#,Asp.net Mvc,Entity Framework,Sorting,Iqueryable,这是MyPageViewModel: public class MyPageViewModel { public Guid Id { get; set; } public string Name { get; set; } public string Surname { get; set; } public string DealerSource { get; set;} public override void CreateMapping()
public class MyPageViewModel
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string DealerSource { get; set;}
public override void CreateMapping()
{
Mapper.CreateMap<MyPage, MyPageViewModel>().
ForMember(dest => dest.DealerSource,opt => opt.MapFrom(w => w.DealerSource.Value));
}
}
这是经销商实体:
[Table("Dealer")]
public partial class Dealer
{
public Guid Id { get; set; }
public string Value { get; set; }
}
MyPage和经销商之间的关系是,经销商可以有许多MyPage
MyPageViewModel显示在一个网格中,该网格可对Id、名称和姓氏列进行排序
这是IQueryable
的OrderBy
扩展方法:
公共静态IQueryable OrderBy(此IQueryable源、字符串sortProperty、ListSortDirection sortOrder)
{
var类型=类型(T);
var property=type.GetProperty(sortProperty);
var参数=表达式参数(类型为“p”);
var propertyAccess=Expression.MakeMemberAccess(参数,属性);
var orderByExp=Expression.Lambda(propertyAccess,参数);
var typeArguments=新类型[]{Type,property.PropertyType};
var methodName=sortOrder==ListSortDirection.Ascending?“OrderBy”:“OrderByDescending”;
var resultextsp=Expression.Call(typeof(Queryable)、methodName、typeArguments、source.Expression、Expression.Quote(orderByExp));
返回source.Provider.CreateQuery(resultExp);
}
问题在于
DealerSource
列。不可能按此对网格进行排序。由于是外键,OrderBy
方法type.GetProperty(sortProperty)
的第二行返回null
。我来这里是为了找到一个解决办法。如果myPage
“可以有许多Dealer
”,为什么DealerSource
导航属性指单个Dealer
?如何使用OrderBy
?什么是T
?如果property
为null
,可能是因为T
上没有同名的属性。对不起。经销商可以有许多MyPages。编辑了问题。@CharlesMager他想做的“DealerSource.Value”
您可能可以使用类似的方法。如果搜索拆分属性名称表达式,您应该会找到大量示例。如果myPage
“可以有许多Dealer
”,为什么DealerSource
导航属性指的是单个Dealer
?如何使用OrderBy
?什么是T
?如果property
为null
,可能是因为T
上没有同名的属性。对不起。经销商可以有许多MyPages。编辑了问题。@CharlesMager他想做的“DealerSource.Value”
您可能可以使用类似的方法。如果搜索拆分属性名称表达式,您应该会找到大量示例。如果myPage
“可以有许多Dealer
”,为什么DealerSource
导航属性指的是单个Dealer
?如何使用OrderBy
?什么是T
?如果property
为null
,可能是因为T
上没有同名的属性。对不起。经销商可以有许多MyPages。编辑了问题。@CharlesMager他想做的“DealerSource.Value”
您可能可以使用类似的方法。如果搜索拆分属性名称表达式,您应该会找到大量的示例。
[Table("Dealer")]
public partial class Dealer
{
public Guid Id { get; set; }
public string Value { get; set; }
}
public static IQueryable<T> OrderBy<T>(this IQueryable<T> source, string sortProperty, ListSortDirection sortOrder)
{
var type = typeof(T);
var property = type.GetProperty(sortProperty);
var parameter = Expression.Parameter(type, "p");
var propertyAccess = Expression.MakeMemberAccess(parameter, property);
var orderByExp = Expression.Lambda(propertyAccess, parameter);
var typeArguments = new Type[] { type, property.PropertyType };
var methodName = sortOrder == ListSortDirection.Ascending ? "OrderBy" : "OrderByDescending";
var resultExp = Expression.Call(typeof(Queryable), methodName, typeArguments, source.Expression, Expression.Quote(orderByExp));
return source.Provider.CreateQuery<T>(resultExp);
}