1

This is the 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));
    }
 }

This is MyPage entity:

[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; }
}

And, this is the Dealer entity:

[Table("Dealer")]
public partial class Dealer
{
    public Guid Id { get; set; } 
    public string Value { get; set; }
}

Relationship between MyPage and Dealer is, Dealer can have many MyPages.

MyPageViewModel is displayed in a grid which is sortable for Id, Name and Surname columns.

This is the OrderBy<T> extension method for 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);
}

The problem is with the DealerSource column. It's not possible to sort the grid by that. As it is foreign key, second line of OrderBy method type.GetProperty(sortProperty) returns null. I've come unstuck to find a workaround.

Jude
  • 2,353
  • 10
  • 46
  • 70
  • If `myPage` "can have many `Dealer`", why does the `DealerSource` Navigation-Property refer to single `Dealer`? – haim770 Jun 09 '15 at 07:38
  • How is `OrderBy` used? What is `T`? If `property` is `null`, it's presumably because there is no property on `T` with that name. – Charles Mager Jun 09 '15 at 07:43
  • Sorry. Dealer can have many MyPages. Edited the question. – Jude Jun 09 '15 at 07:45
  • @CharlesMager T is MyPage. Yes, mentally there shouldn't be any property with that name based on the truth of returning null. But we have DealerSource property in MyPageViewModel which was mapped to Dealer.Value – Jude Jun 09 '15 at 07:48
  • @Jude I'm not sure I follow. I've copied your class definitions to prove, and `typeof(MyPage).GetProperty("DealerSource")` does not return `null`. – Charles Mager Jun 09 '15 at 07:53
  • 1
    @CharlesMager He wants to do `"DealerSource.Value"` – xanatos Jun 09 '15 at 07:53
  • @xanatos aha! just me being dumb, then. – Charles Mager Jun 09 '15 at 07:54
  • 1
    You can probably use something like http://stackoverflow.com/a/16545157/613130 . If you search *split property name expression* you should find plenty of examples around. – xanatos Jun 09 '15 at 07:56

0 Answers0