0

i have one method and it will return IQueryable value

public IQueryable<TabMasterViewModel> Query(Expression<Func<TabMaster, bool>> whereCondition)
{
    IQueryable<TabMaster> tabmasters = _tabmasterRepository.GetQueryable().Where(whereCondition);
    IQueryable<TabMasterViewModel> tabmasterview;
    AutoMapper.Mapper.CreateMap<TabMaster, TabMasterViewModel>()
        .ForMember(dest => dest.colID, opt => opt.MapFrom(src => src.colID));
    tabmasterview = AutoMapper.Mapper.Map(tabmasters, tabmasterview);
    return tabmasterview;
}

and the GetQueryable is like

public IQueryable<T> GetQueryable()
{
    return this.ObjectSet.AsQueryable<T>();
}

but the following line

tabmasterview = AutoMapper.Mapper.Map(tabmasters, tabmasterview);

is giving me an error

Use of unassigned local variable 'tabmasterview'

please suggest me, where is am i wrong?

1 Answer 1

1

To fix the compiler error all you have to do is

 IQueryable<TabMasterViewModel> tabmasterview = null;

But the second argument you are passing to AutoMapper.Mapper.Map(tabmasters, tabmasterview) is always null as tabMasterview has not been intialized. If that is intended you could as well pass "null".

public IQueryable<TabMasterViewModel> Query(Expression<Func<TabMaster, bool>> whereCondition)
        {
            IQueryable<TabMaster> tabmasters = _tabmasterRepository.GetQueryable().Where(whereCondition);

            AutoMapper.Mapper.CreateMap<TabMaster, TabMasterViewModel>()
                  .ForMember(dest => dest.colID, opt => opt.MapFrom(src => src.colID));
            return AutoMapper.Mapper.Map<IQueryable<TabMaster>,  IQueryable<TabMasterViewModel>> (tabmasters);            
        }

EDIT:

The above code answers your question related to compiler error. As for your comments about the runtime error.. modify this line

return AutoMapper.Mapper.Map<IQueryable<TabMaster>,  IQueryable<TabMasterViewModel>> (tabmasters);   

to

return AutoMapper.Mapper.Map<IQueryable<TabMaster>,  IEnumerable<TabMasterViewModel>> (tabmasters).AsQueryable<TabMasterViewModel>();   
Sign up to request clarification or add additional context in comments.

5 Comments

runtime error like: Unable to cast object of type 'System.Collections.Generic.List1[Models.TabMasterViewModel]' to type 'Linq.IQueryable1[Models.TabMasterViewModel]'. my updated code like 'public IQueryable<TabMasterViewModel> IQueryable<TabMaster> tabmasters = _tabmasterRepository.GetQueryable().Where(whereCondition); IQueryable<TabMasterViewModel> tabmasterview = null; AutoMapper.Mapper.CreateMap<TabMaster, TabMasterViewModel>() .ForMember(dest => dest.colID, opt => opt.MapFrom(src => src.colID)); tabmasterview = AutoMapper.Mapper.Map(tabmasters, tabmasterview); return tabmasterview;
Yes. Check the updated code. Basically you are asking the AutoMapper to map an IQuerable of TabMaster's to Iquerable TabMasterViewModel.
same error comes again: Unable to cast object of type 'System.Collections.Generic.List1[CAFM.Business.Models.TabMasterViewModel]' to type 'System.Linq.IQueryable1[CAFM.Business.Models.TabMasterViewModel]'.
I'd added code under edit section which should solve the run-time issue.
Hi dude you are absolutely right and now i am getting 100% success. Thanks a lot. my vote is +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.