2017-10-08 93 views
0

为什么我有这个错误,我该如何解决它ASP.NET MVC-不能隐式地将类型'System.Collections.Generic.IEnumerable <>'转换为'System.Collections.Generic.List <System.Tuple <int,string>>

错误9无法隐式转换类型 'System.Collections.Generic.IEnumerable < BPP.CCSP.Admin.Infrastructure.COUNTRIES>' 到“System.Collections.Generic.List < System.Tuple <整型,字符串> >”。存在明确的转换(您是否缺少转换?)C:\ Users \ JKK-HP \ Documents \ Visual Studio 2013 \ Projects \ BPP.CCSP \ BPP.CCSP.Admin.Repository \ Managers \ Concrete \ AdminManager.cs 234 24 BPP.CCSP.Admin.Repository

public List<Tuple<int, string>> getOptionList(string p) 
{ 
    if (p == "country") 
    { 
     return _countriesRepository.FindAll(); 
    } 
} 
+0

因为'_countriesRepository.FindAll()'不是'List >类型的类型,所以您需要投射 – Munzer

回答

0

添加到ToList打电话到您的通话FindAll,就像这样:

return _countriesRepository.FindAll().ToList(); 

这将转换您的IEnumerable<T>FindAll返回到List<T>,这是从getOptionList返回的是什么。

这只有在两种情况下T都相同时才有效。即如果FindAll返回IEnumerable<Tuple<int, string>>。如果情况并非如此,则一种选择是考虑使用Select然后ToList。例如:

return _countriesRepository.FindAll().Select(...).ToList(); 

注意:如果这将是一个大列表,您可能需要考虑某种形式的分页。

相关问题