C# ASP.NET MVC错误无法将类型数据模型隐式转换为实体
我正在ASP.NETMVC中制作一个天气应用程序,我遇到了无法解决的错误。 第一个错误: 无法隐式转换类型C# ASP.NET MVC错误无法将类型数据模型隐式转换为实体,c#,asp.net-mvc-4,C#,Asp.net Mvc 4,我正在ASP.NETMVC中制作一个天气应用程序,我遇到了无法解决的错误。 第一个错误: 无法隐式转换类型 System.Collections.Generic.IEnumerable 到 System.Collections.Generic.IEnumerable。 存在显式转换(是否缺少强制转换?) 第二个错误: 无法隐式转换类型 System.Collections.Generic.IEnumerable 到 System.Collections.Generic.IEnumerable。
System.Collections.Generic.IEnumerable
到
System.Collections.Generic.IEnumerable
。
存在显式转换(是否缺少强制转换?)
第二个错误:
无法隐式转换类型
System.Collections.Generic.IEnumerable
到
System.Collections.Generic.IEnumerable
。
存在显式转换(是否缺少强制转换?)
代码:
public覆盖IEnumerable GetLocation(string locationName)
{
var locations=_iWeatherRepository.FindLocationByName(locationName);
if(locations==null | | locations.Count()==0)
{
locations=\u geoNameWebservice.GetLocation(locationName);
foreach(位置中的var位置)
{
_iWeatherRepository.AddLocation(位置);
}
_iWeatherRepository.Save();
}
返回地点;
}
我缺少什么类型的演员?由于类型不匹配,您会收到这些错误 第一件事。您正在将
位置设置为变量。
除非\u iWeatherRepository.FindLocationByName(locationName)
和\u geoNameWebservice.GetLocation(locationName)代码>两者都返回相同的类型,如果其类型是隐含的,则不能将两者都设置为位置
\u geoNameWebservice.GetLocation(locationName)
返回System.Collections.Generic.IEnumerable
,因此需要转换/映射其项以匹配预期类型
第一个错误意味着\u iWeatherRepository.FindLocationByName(locationName)
返回System.Collections.Generic.IEnumerable.
第二个错误意味着方法的返回类型是System.Collections.Generic.IEnumerable
您需要显式地声明要返回的类型,以避免混淆编译器
public override IEnumerable<WeatherService.Domain.DataModels.Location> GetLocation(string locationName)
{
IEnumerable<WeatherService.Domain.DataModels.Location> locations = _iWeatherRepository.FindLocationByName(locationName);
if (locations == null || locations.Count() == 0)
{
var entities = _geoNameWebservice.GetLocation(locationName);
foreach (var entity in entities)
{
var location = new WeatherService.Domain.DataModels.Location();
//Copy the properties over to your domain model
location.Property1 = entity.Property1;
//...code removed for brevity
_iWeatherRepository.AddLocation(location);
}
_iWeatherRepository.Save();
locations = _iWeatherRepository.FindLocationByName(locationName)
}
return locations;
}
public覆盖IEnumerable GetLocation(string locationName)
{
IEnumerable locations=_iWeatherRepository.FindLocationByName(locationName);
if(locations==null | | locations.Count()==0)
{
var entities=\u geoNameWebservice.GetLocation(locationName);
foreach(实体中的var实体)
{
var location=new WeatherService.Domain.DataModels.location();
//将属性复制到域模型
location.Property1=entity.Property1;
//…为简洁起见,删除了代码
_iWeatherRepository.AddLocation(位置);
}
_iWeatherRepository.Save();
locations=\u iWeatherRepository.FindLocationByName(locationName)
}
返回地点;
}
您可以使用类似于“自动映射”的工具将实体的属性映射到域模型。由于类型不匹配,您会遇到这些错误。谢谢您的回答。我真的觉得自己很笨……我只是不明白为什么我要说出这些类型?两个位置类为部分类(属于同一类);entity.location是我的模型,datamodel.location是框架创建的实体。框架不应该知道吗?这两个位置类在不同的名称空间中。这就是为什么。
return locations;
public override IEnumerable<Location> GetLocation(string locationName)
{
var locations = _iWeatherRepository.FindLocationByName(locationName);
if (locations == null || locations.Count() == 0)
{
locations = _geoNameWebservice.GetLocation(locationName);
foreach (var location in locations)
{
_iWeatherRepository.AddLocation(location);
}
_iWeatherRepository.Save();
}
return locations;
}
public override IEnumerable<WeatherService.Domain.DataModels.Location> GetLocation(string locationName)
{
IEnumerable<WeatherService.Domain.DataModels.Location> locations = _iWeatherRepository.FindLocationByName(locationName);
if (locations == null || locations.Count() == 0)
{
var entities = _geoNameWebservice.GetLocation(locationName);
foreach (var entity in entities)
{
var location = new WeatherService.Domain.DataModels.Location();
//Copy the properties over to your domain model
location.Property1 = entity.Property1;
//...code removed for brevity
_iWeatherRepository.AddLocation(location);
}
_iWeatherRepository.Save();
locations = _iWeatherRepository.FindLocationByName(locationName)
}
return locations;
}