none
Convert Func<IQueryable> and IOrderedQueryable from view model to model RRS feed

  • Question

  • Hi,
        Am having hard time in converting the below expression

    from

    Expression <Func<IQueryable<tblObjectPermissionViewModel>, IOrderedQueryable<tblObjectPermissionViewModel>>> orderby

    to

    Expression <Func<IQueryable<tblObjectPermission>, IOrderedQueryable<tblObjectPermission>>> orderby

    Any help is appreciated

    Thanks
    Roopesh


    Roopesh

    Wednesday, August 3, 2016 4:29 PM

Answers

  • Hi Roopesh Kumar,

    Please check the following code, If type tblObjectPermission can be casted to the type tblObjectPermissionViewModel, it should work.

    public class Test
    {
        public IEnumerable<tblObjectPermissionViewModel> Get<tblObjectPermissionViewModel, tblObjectPermission>(Func<IQueryable<tblObjectPermissionViewModel>, IOrderedQueryable<tblObjectPermissionViewModel>> orderBy = null)
        {
            IQueryable<tblObjectPermissionViewModel> query = new List<tblObjectPermission>().Select(t => Convert<tblObjectPermissionViewModel, tblObjectPermission>(t)).AsQueryable();
            return orderBy(query).AsEnumerable(); // Convert orderBy to tblObjectPermission.
        }
    
        public tblObjectPermissionViewModel Convert<tblObjectPermissionViewModel, tblObjectPermission>(tblObjectPermission input) where tblObjectPermissionViewModel: class
        {
            return input as tblObjectPermissionViewModel;
        } 
    }

    In addition, you could also use AutoMapper to achieve it. like this:

    // Initialize AutoMapper
    Mapper.Initialize(cfg => 
    { 
        cfg.CreateMap<tblObjectPermission, tblObjectPermissionViewModel>();
        cfg.CreateMap<tblObjectPermissionViewModel, tblObjectPermission>();
    });
    
    public class Test
    {
        // Wrap Func with Expression
        public IEnumerable<tblObjectPermissionViewModel> Get(Expression<Func<IQueryable<tblObjectPermissionViewModel>, IOrderedQueryable<tblObjectPermissionViewModel>>> orderBy = null)
        {
            var orderByExpr = Mapper.Map<Expression<Func<IQueryable<tblObjectPermission>, IOrderedQueryable<tblObjectPermission>>>>(orderBy);            
            IQueryable<tblObjectPermission> query = CurrentDbSet;
    
            // Compile expression and execute as function 
            var items = orderByExpr.Compile()(query).ToList();
    
            // Map back to list of tblObjectPermissionViewModel
            return Mapper.Map<IEnumerable<tblObjectPermissionViewModel>>(items);
        }
    }

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, August 4, 2016 5:15 AM
    Moderator
  • Thanks Cole Wu. It did work for me, but I have to change my solution as am not having the DBContext / DBSet in my library. So I ended up changing my approach. I really appreciate your time and help.

    Thanks
    Roopesh


    Roopesh

    • Marked as answer by Roopesh Kumar Thursday, August 4, 2016 8:42 PM
    Thursday, August 4, 2016 8:42 PM

All replies

  • Hi Roopesh Kumar,

    Please check the following code, If type tblObjectPermission can be casted to the type tblObjectPermissionViewModel, it should work.

    public class Test
    {
        public IEnumerable<tblObjectPermissionViewModel> Get<tblObjectPermissionViewModel, tblObjectPermission>(Func<IQueryable<tblObjectPermissionViewModel>, IOrderedQueryable<tblObjectPermissionViewModel>> orderBy = null)
        {
            IQueryable<tblObjectPermissionViewModel> query = new List<tblObjectPermission>().Select(t => Convert<tblObjectPermissionViewModel, tblObjectPermission>(t)).AsQueryable();
            return orderBy(query).AsEnumerable(); // Convert orderBy to tblObjectPermission.
        }
    
        public tblObjectPermissionViewModel Convert<tblObjectPermissionViewModel, tblObjectPermission>(tblObjectPermission input) where tblObjectPermissionViewModel: class
        {
            return input as tblObjectPermissionViewModel;
        } 
    }

    In addition, you could also use AutoMapper to achieve it. like this:

    // Initialize AutoMapper
    Mapper.Initialize(cfg => 
    { 
        cfg.CreateMap<tblObjectPermission, tblObjectPermissionViewModel>();
        cfg.CreateMap<tblObjectPermissionViewModel, tblObjectPermission>();
    });
    
    public class Test
    {
        // Wrap Func with Expression
        public IEnumerable<tblObjectPermissionViewModel> Get(Expression<Func<IQueryable<tblObjectPermissionViewModel>, IOrderedQueryable<tblObjectPermissionViewModel>>> orderBy = null)
        {
            var orderByExpr = Mapper.Map<Expression<Func<IQueryable<tblObjectPermission>, IOrderedQueryable<tblObjectPermission>>>>(orderBy);            
            IQueryable<tblObjectPermission> query = CurrentDbSet;
    
            // Compile expression and execute as function 
            var items = orderByExpr.Compile()(query).ToList();
    
            // Map back to list of tblObjectPermissionViewModel
            return Mapper.Map<IEnumerable<tblObjectPermissionViewModel>>(items);
        }
    }

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, August 4, 2016 5:15 AM
    Moderator
  • Thanks Cole Wu. It did work for me, but I have to change my solution as am not having the DBContext / DBSet in my library. So I ended up changing my approach. I really appreciate your time and help.

    Thanks
    Roopesh


    Roopesh

    • Marked as answer by Roopesh Kumar Thursday, August 4, 2016 8:42 PM
    Thursday, August 4, 2016 8:42 PM