【发布时间】:2016-12-29 23:23:49
【问题描述】:
我正在 ASP.NET MVC 5 中创建一个 Web 应用程序,并且需要使用数据库获取一些数据。获得数据后,我想使用它创建一个IPagedList 对象。但是,我发现此功能会生成一个异常,指出Type CurrentSite is not supported in LINQ to Entities。反之,仅获取数据而不创建 IPagedList 时,则不会发生这种情况。
public ActionResult GetComputers(int? SiteID)
{
//This works
IQueryable<Computer> computers = db.Computers.Where(c => c.CurrentSite.ID == SiteID);
//This doesn't - It throws the LINQ to Entities exception
IPagedList<Computer> computers = db.Computers.Where(c => c.CurrentSite.ID == SiteID).ToPagedList(1, 25);
}
如何在不向我的数据库表中添加CurrentSite 列的情况下使用获得的数据创建IPagedList 对象?
更新:
这是CurrentSite 属性背后的逻辑。
public ComputerSites CurrentDeployment
{
get
{
if (this.ComputerSites == null)
{
return null;
}
else
{
return this.ComputerSites.Where(cs => DateTime.Now.IsBetween(cs.StartDate, cs.EndDate) && cs.Deleted == false).OrderBy(cs => cs.StartDate).FirstOrDefault();
}
}
}
public Site CurrentSite
{
get
{
if (this.CurrentDeployment == null)
{
return null;
}
else
{
return this.CurrentDeployment.Site;
}
}
}
【问题讨论】:
-
你的 IPagedList 来自哪里?
-
db.Computers.Where(c => c.CurrentSite.ID == SiteID).ToList();工作吗? -
@AdilMammadov 我刚刚尝试过这个并得到了同样的错误,即使在订购查询时也是如此。
-
您应该更新您的问题并发布您的模型。我觉得你的模型有问题。
-
我建议你改变逻辑。您的模型看起来不正确。
标签: c# asp.net database entity-framework linq