在 Controller 中,return 记录 [verified_date] 从当前日期起超过 3 个月?

In Controller, return records with [verified_date] older than 3 months from current date?

我正在我的 MVC5 Code-First 网络应用程序中处理验证资产视图,并且我正在尝试(默认情况下)return 仅 INV_Assets 中具有 [verified_date] 从当前 Date.

开始超过 3 个月

我尝试了以下操作,但收到

LINQ to Entities does not recognize the method 'System.DateTime AddMonths(Int32)' method, and this method cannot be translated into a store expression

加载视图时出错:

public class VerifyAssetsController : Controller
{
    private InventoryTrackerContext db = new InventoryTrackerContext();

    // GET: VerifyAssets
    public async Task<ActionResult> Index()
    {
        var iNV_Assets = db.INV_Assets.Where(i => i.verified_date < DateTime.Now.AddMonths(-3)).Include(i => i.Location).Include(i => i.Manufacturer).Include(i => i.Model).Include(i => i.Status).Include(i => i.Type).Include(i => i.Vendor);
        return View(await iNV_Assets.ToListAsync());
    }

任何人都可以提供一些关于如何解决这个问题的见解吗?

理想情况下,我希望默认值为从当前日期起 3 个月后,然后在视图上显示 DropDowns,这将允许用户指定 1-12 Months/Weeks/Days 从当前 Date.

不是在查询中计算上个月,而是在查询之前获取该值并像这样传递它:

var previousDate = DateTime.Now.AddMonths(-3);
var iNV_Assets = db.INV_Assets
                   .Where(i => i.verified_date < previousDate)
                   .Include(i => i.Location)
                   .Include(i => i.Manufacturer)
                   .Include(i => i.Model)
                   .Include(i => i.Status)
                   .Include(i => i.Type)
                   .Include(i => i.Vendor);

(我不确定你为什么要添加所有 Include 语句,如果你只打算 select Assets,你可以去掉它们)

您的代码失败的原因是它试图将 DateTime.Now.AddMonths(-3) 转换为底层数据源语言 (可能是 SQL) 但失败了在这样做。