【问题标题】:Return IQueryable<string>返回 IQueryable<字符串>
【发布时间】:2012-07-18 18:57:10
【问题描述】:

我正在使用 pageList(由 troy goode 制作的那个),在他的例子中他有

// in this case we return IEnumerable<string>, but in most
// - DB situations you'll want to return IQueryable<string>
private IEnumerable<string> GetStuffFromDatabase()
{
    var sampleData = new StreamReader(Server.MapPath("~/App_Data/Names.txt")).ReadToEnd();
    return sampleData.Split('\n');
}

由于我使用的是数据库,因此我更改为IQueryable,但是,我不知道在里面写什么来从数据库中返回数据,我尝试将路径更改为 ~/App_Data/DatabaseName.sdf 但是我明白了sampleData.Split('\n');

cannot implicitly convert type string to system.linq.iqueryable&lt;string&gt;

我该如何改变?

【问题讨论】:

    标签: asp.net asp.net-mvc pagedlist


    【解决方案1】:
    return sampleData.Split('\n').AsQueryable();
    

    【讨论】:

    • 这部分代码public ActionResult Index(int ? page) { var listPaged = GetPagedNames(page); if (listPaged == null) { return HttpNotFound(); } ViewBag.Names = listPaged; return View(db.Productos.ToList()); }
    • Give me "DataException was unhandled by user code, an exception occurred while initializing the database. See the inner exception for details" this error on the line of this line return View(db.Productos.ToList());
    • 您可能应该更改问题中的代码以反映您实际使用的代码,以便我们为您提供帮助。
    【解决方案2】:

    你的方法的返回类型是 IEnumerable 并且需要作为枚举返回

    所以请这样做

    return sampleData.Split('\n').AsEnumerable();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-17
      • 1970-01-01
      • 1970-01-01
      • 2011-02-27
      • 2019-03-22
      • 2020-08-07
      相关资源
      最近更新 更多