imdadhusen
imdadhusen

Reputation: 2494

Not able to write proper function in asp.net mvc 3.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?

Upvotes: 0

Views: 224

Answers (1)

Vijay Sirigiri
Vijay Sirigiri

Reputation: 4711

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>();   

Upvotes: 1

Related Questions