接触MVC不久,在项目中遇到了下面的问题:    有一张表Customers,我现在只想取出其中的一个字段ContactName在List页面展示出来,代码如下。
public ActionResult Index()
{
    var q = from c in ltdb.Customers select c.ContactName;    return View(q);
}
    但是代码执行过程中会报错,错误如下。
传入字典的模型项的类型为“System.Data.Linq.DataQuery'1[System.String]”,但此字典需要类型“System.Collections.Generic.IEnumerable'1[MvcApplication3.Models.Customers]”的模型项。    现在是不是需要将“from c in ltdb.Customers select c.ContactName”的结果转换成为“IEnumerable<T>”类型返回呢,    如果是的话,怎么将其转换成为正确的模型项?麻烦高手帮忙指点一下,不胜感激。

解决方案 »

  1.   

    当前页面接受的是IEnumerable<MvcApplication3.Models.Customers>页面我没改动过,是在Index上直接添加得到的。我就是想知道怎么把“var q = from c in ltdb.Customers select c.ContactName;”得到的结果转换成为IEnumerable<MvcApplication3.Models.Customers>
    类型
      

  2.   

    我没用过Linq,但是我觉得报的这个错误的原因是你定义的是IEnumerable<MvcApplication3.Models.Customers>和你赋值的是IEnumerable<string>是不对的。你把IEnumerable<MvcApplication3.Models.Customers>改成IEnumerable<string>试试。
      

  3.   

    类型选择错误吧?c.ContactName是什么类型的?返回的是什么类型?
      

  4.   

    我现在就想知道怎么把查询结果构建成IEnumerable<MvcApplication3.Models.Customers>类型,
    就是这样,请高手指点下。
      

  5.   

    三楼的正解
    好像不支持强制转换,一个是跟数据库表格相对应的多列类型IEnumerable'1[MvcApplication3.Models.Customers]的列表  一个只有一个STRING列的列表IEnumerable<string>对应不上来所以转换不了,如果确实只想传递类似数据库里的几列 要自己构建个模型  个人愚见 请高手赐教