这是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()
{
Mapper.CreateMap<MyPage, MyPageViewModel>().
ForMember(dest => dest.DealerSource,opt => opt.MapFrom(w => w.DealerSource.Value));
}
}
这是MyPage实体:
[Table("MyPage")]
public partial class MyPage
{
public Guid Id { get; set; }
public Guid DealerId { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public virtual Dealer DealerSource { get; set; }
}
而且,这是经销商实体:
[Table("Dealer")]
public partial class Dealer
{
public Guid Id { get; set; }
public string Value { get; set; }
}
MyPage和经销商之间的关系是,经销商可以有很多MyPages。
MyPageViewModel显示在网格中,可以对Id,Name和Surname列进行排序。
这是OrderBy<T>
的{{1}}扩展方法:
IQueryable<T>
问题在于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);
}
列。由此无法对网格进行排序。因为它是外键,DealerSource
方法OrderBy
的第二行返回type.GetProperty(sortProperty)
。我找不到解决办法了。