LINQ to Entities does not recognize the method 'AddMonths()' method

Thảo luận trong 'Lập Trình Website MVC5 & MVC6' bắt đầu bởi seolagi, 15/12/20.

  1. seolagi
    Tham gia ngày:
    16/4/14
    Bài viết:
    1,029
    Đã được thích:
    80
    Điểm thành tích:
    48
    Web mình bị lỗi lệnh LINQ mà mình kiểm tra thấy rất bình thường? bạn nào biết giúp mình với. Nó báo lỗi như sau:
    LINQ to Entities does not recognize the method 'System.DateTime AddMonths(Int32)' method, and this method cannot be translated into a store expression.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
    Exception Details: System.NotSupportedException: LINQ to Entities does not recognize the method 'System.DateTime AddMonths(Int32)' method, and this method cannot be translated into a store expression.

    Code của mình:
    Mã:
    var model = dbc.Products
        .Where(p => p.Available 
        && p.UnitPriceAgencyOld != null && p.UnitPriceAgencyOld > 0 && p.UnitPriceAgencyOld != p.UnitPriceAgency && p.DateUpUnitPriceAgency >= DateTime.Now.AddMonths(-1));
    
    Mình đã thử đổi lại, adddays, addyears đều bị lỗi tương tự.

    linq-to-entities-does-not-recognize-the-method-addmonths.jpg
     
    Cảm ơn đã xem bài:

    LINQ to Entities does not recognize the method 'AddMonths()' method

  2. admin
    Tham gia ngày:
    22/5/13
    Bài viết:
    4,897
    Đã được thích:
    1,198
    Điểm thành tích:
    113
    Giới tính:
    Nam
    Nó có thông báo là cái AddMonths không được hỗ trợ trong câu lệnh linQ ý bạn, bạn chỉ cần tách nó ra là được ngay.

    Sửa lại thành:
    Mã:
    var datenow = DateTime.Now.AddMonths(-1);
    var model = dbc.Products
        .Where(p => p.Available
        && p.UnitPriceAgencyOld != null && p.UnitPriceAgencyOld > 0 && p.UnitPriceAgencyOld != p.UnitPriceAgency && p.DateUpUnitPriceAgency >= datenow);
     
  3. seolagi
    Tham gia ngày:
    16/4/14
    Bài viết:
    1,029
    Đã được thích:
    80
    Điểm thành tích:
    48
    Dạ em cảm ơn ạ, em quên không để ý.
     


Chủ để tương tự : LINQ Entities
Diễn đàn Tiêu đề Date
Lập Trình Website MVC5 & MVC6 Lỗi: The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities. 27/3/20
Lập Trình Website MVC5 & MVC6 The entity or complex type cannot be constructed in a LINQ to Entities query 12/12/19
Lập Trình Website MVC5 & MVC6 Clear cache in Linq, xóa Cache lưu trong Linq asp.net MVC 1/7/22
Lập Trình Website MVC5 & MVC6 Delete Multiple records with Entity Framework Linq ASP.Net MVC 5 20/4/20
Lập Trình Website MVC5 & MVC6 Lấy giá trị Max và Min trong LINQ MVC C#, get value Min Max in LinQ MVC 2/3/20