由于我使用了以下查询太多,我决定创建一个类和一个函数,它返回三个表中的一些属性,如下所示;
public class EntityHandler
{
public IQueryable<Entity.MedicalEntities> GetAll(Entity.MedicalEntities Entity)
{
IQueryable<Entity.MedicalEntities> result = from mID in Entity.Medicals
join cs in Entity.Cities on mID.CityFK equals cs.CityID
join reg in Entity.Regions on cs.RegionFK equals reg.RegionID
select new
{
medicalName = mID.medicalName,
MedicalID = mID.MedicalID,
CityName = cs.CityName,
CityID = cs.CityID,
RegionName = reg.RegionName,
RegionID = reg.RegionID
};
return result;
}
}
但我收到以下错误;
Error1 Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>'
to 'System.Linq.IQueryable<MedicalBootStrap.Entity.MedicalEntities>'. An explicit
conversion exists (are you missing a cast?)
你知道如何克服这个问题吗?
答案 0 :(得分:2)
您没有创建Entity.MedicalEntities
类型的对象。因此,您需要更改select语句,将匿名类型返回到强类型实体。
public IQueryable<Entity.MedicalEntities> GetAll(Entity.MedicalEntities Entity)
{
IQueryable<Entity.MedicalEntities> result = from mID in Entity.Medicals
join cs in Entity.Cities on mID.CityFK equals cs.CityID
join reg in Entity.Regions on cs.RegionFK equals reg.RegionID
//insert actual real entity name for Entity.MedicalEntity
select new Entity.MedicalEntity
{
medicalName = mID.medicalName,
MedicalID = mID.MedicalID,
CityName = cs.CityName,
CityID = cs.CityID,
RegionName = reg.RegionName,
RegionID = reg.RegionID
};
return result;
}
<强> //修改
我想你忘记了问题中的一些信息,比如三个原始表和你创建的类。我假设3个表格是Medicals
Cities
&amp; Regions
和
MedicalEntities
表示您使用它创建的ViewModel。