2016-05-11 43 views
-1

我的控制器文件包含如下。无法将类型'System.Data.Linq.ISingleResult <"">'隐式转换为'System.Collections.Generic.IEnumerable <" ">。显式转换存在?

public ActionResult Index() 
{ 
    CityNameList ct = dt.AllCity(); /*Returns Citynames. Used Linq to Sql Classes to retrieve from DB. */ 

    return View(ct); 
} 

而在CityNameList.cs文件中型号

public class CityNameList 
{ 
    public IEnumerable<string> CityName {get;set;} 
} 

真的尽力了很多解决这个问题,但没有用。 我是MVC的新手。我需要清楚的想法将数据从控制器中的存储过程的结果传递到View。

+0

请提供AllCity中的代码() – JDupont

+0

您可以添加有关您的程序的更多信息以及您收到错误消息的哪一行?我假设你试图将一个类型为SingleResult的值赋给CityName,这个值是IEnumerable ,但是你没有提供太多的信息。 – Auguste

+0

@JDupont - AllCity()是通过Linq调用到Sql类的存储过程的名称,dt是L2SQL类中的对象od数据上下文.............还有一件事我需要改变CityNmaeList.cs文件中的任何内容.. ?? – Abhi

回答

0

看起来像你正在使用LinqToSql。如果存储过程返回ISingleResult,那么您需要枚举结果以获取数据。您可以调用ToList()将结果转换为IEnumerable。此外,它看起来像

var result = dt.AllCity(); 
CityNameList ct = null; 
if (result != null && result.Any()) { 
    ct = new CityNameList { CityName = result.ToList()}; 
} 
return View(ct); 
+0

@ vendettamit - 没有用..same错误越来越喜欢 - 不能隐式转换类型'System.Collections.Generic.List '为'System.Collections.Generic.IEnumerable '。存在明确的转换(你是否缺少转换?).....所有我需要映射存储过程输出到CityNamelist.cs类强类型视图生成.. – Abhi

-1

变化CityNameList类属性如下:

public class CityNameList 
    {  
     public string CityName{ get; set; } 
    } 

并添加另一个类存储过程输出映射到类CityNameList像下面。

public class CityDataContext 
{ 
    DataDataContext dt = new DataDataContext(); 
    public IEnumerable<CityNameList> DisplayName 
    { 
     get 
     { 
      List<CityNameList> Details = new List<CityNameList>(); 
      var result = dt.AllCity().ToList(); 
      for (int j = 0; j < result.Count;j++) 
      { 
       CityNameList city = new CityNameList(); 
       city .CityName= Convert.ToString(result[j].cityname); 

       Details.Add(city); 
      } 
      return Details; 
     } 
    } 
} 

凡内的循环城市名是表将在DB执行存储过程后返回的列名,最后加入用模型类CityNameList.It强类型视图将做工精细....

相关问题