存在MVC 5隐式转换

时间:2015-12-18 17:55:38

标签: asp.net-mvc-5

我有一个存储库,我要将查询传递给控制器​​。

 public ProjectViewModel SearchContractors(string zip)
 {
     var query = (from h in repository.tblHandymen
                  join hc in repository.tblHandyManCoverages on h.handymanID equals hc.handymanID
                  join s in repository.tblServiceRequests on hc.zip equals s.zip
                  where hc.zip == zip
                  where h.handymanID == hc.handymanID
                  where h.status == "Active"
                  select h
                  );
     ProjectViewModel model = new ProjectViewModel
     {
         ContractorSearch = query.AsEnumerable()                                 
     };

     return model;
 }

我被困在哪里

ProjectViewModel model = new ProjectViewModel
{
    ContractorSearch = query.AsEnumerable()                                 
};

错误是存在隐式转换。尝试了几件事。没什么办法。

1 个答案:

答案 0 :(得分:0)

您的查询未返回ProjectContractorSearchViewModel个对象的集合(它返回tblHandymen的集合)。您需要将结果投影到typeof ProjectContractorSearchViewModel

var query = (from h in repository.tblHandymen
             .....
            ).Select(x => new ProjectContractorSearchViewModel
            {
                someProperty = x.someProperty,
                anotherProperty = x.anotherProperty,
                ....
            });
ProjectViewModel model = new ProjectViewModel
{
    ContractorSearch = query.AsEnumerable()                            
};
return model;