locked
error "LINQ to Entities does not recognize the method" when trying to retrieve record for DetailsView in VB code behind RRS feed

  • Question

  • User759549661 posted

    Hi,

    I have a page where I’m trying to add a DetailsView (ID="License") control to conditionally display license information, if the user has a license.  The control has ItemType="LabEle3.OrderDetail" SelectMethod ="GetThisUserLicense" to populate it.

    The code behind is:

        Public Function GetThisUserLicense() As IQueryable(Of OrderDetail)

            Return UserLicenseDetails_GetData.AsQueryable

        End Function

     

    Public Function UserLicenseDetails_GetData() As IList(Of OrderDetail)

            Dim list As New List(Of OrderDetail)()

            Dim ThisUser As String = User.Identity.Name

            Dim ThisUserLicenseInfo As New OrderDetail()

            Dim pdb As New LicenseContext

     

            If Not ThisUser = "" Then

                Try

                    Dim userLicense = (From c In pdb.OrderDetails Where c.Username = ThisUser AndAlso c.CategoryId = 2 Select c).LastOrDefault

                    If userLicense IsNot Nothing Then

                        License.Visible = True

                        ThisUserLicenseInfo.InitDate = CType(userLicense.InitDate, Date)

                        ThisUserLicenseInfo.ExpDate = CType(userLicense.ExpDate, Date)

                        ThisUserLicenseInfo.UnitPrice = CType(userLicense.UnitPrice, String)

                        ThisUserLicenseInfo.Credit = CType((DateDiff(DateInterval.Day, ThisUserLicenseInfo.InitDate, ThisUserLicenseInfo.ExpDate) * ThisUserLicenseInfo.UnitPrice / 365), String)

                    Else

                        License.Visible = False

                    End If

                    list.Add(ThisUserLicenseInfo)

                Catch exp As Exception

                    Throw New Exception("ERROR: Unable to find User License - " & exp.Message.ToString(), exp)

                End Try

            Else

            End If

            Return list

        End Function

    The code chokes on trying to retrieve the record from the table OrderDetails:

    Dim userLicense = (From c In pdb.OrderDetails Where c.Username = ThisUser AndAlso c.CategoryId = 2 Select c).LastOrDefault

    The error is:

    LINQ to Entities does not recognize the method 'LabEle3.OrderDetail LastOrDefault[OrderDetail](System.Linq.IQueryable`1[LabEle3.OrderDetail])' method, and this method cannot be translated into a store expression.

    The line checks out with intellisense.

    The relevant Class definitions are:

    Imports System.Data.Entity

     

    Public Class LicenseContext

        Inherits DbContext

        Public Sub New()

            MyBase.New("LabEle3")

        End Sub

        Public Property OrderDetails As DbSet(Of OrderDetail)

    End Class

    And:

    Public Class OrderDetail

     

        Public Property OrderDetailId() As Integer

        Public Property OrderId() As Integer

        Public Property Username() As String

        Public Property ProductId() As Integer

        Public Property Quantity() As Integer

        Public Property UnitPrice() As Nullable(Of Double)

        Public Property CategoryId() As Integer

        Public Property ProductName() As String

        Public Property IPaddress() As String

        Public Property Seats() As Int16

        Public Property InitDate() As Date

        Public Property ExpDate() As Date

        Public Property Credit() As Nullable(Of Double)

     

     

    End Class

    The table T-SQL is:

    CREATE TABLE [dbo].[OrderDetails] (

        [OrderDetailId] INT            IDENTITY (1, 1) NOT NULL,

        [OrderId]       INT            NOT NULL,

        [Username]      NVARCHAR (MAX) NULL,

        [ProductId]     INT            NOT NULL,

        [Quantity]      INT            NOT NULL,

        [UnitPrice]     FLOAT (53)     NULL,

        [CategoryId]    INT            NOT NULL,

        [ProductName]   NVARCHAR (MAX) NULL,

        [IPaddress]     NVARCHAR (MAX) NULL,

        [Seats]         SMALLINT       NOT NULL,

        [InitDate]      DATE           NOT NULL,

        [ExpDate]       DATE           NOT NULL,

        [Credit]        FLOAT (53)     NULL,

        CONSTRAINT [PK_dbo.OrderDetails] PRIMARY KEY CLUSTERED ([OrderDetailId] ASC),

        CONSTRAINT [FK_dbo.OrderDetails_dbo.Orders_OrderId] FOREIGN KEY ([OrderId]) REFERENCES [dbo].[Orders] ([OrderId]) ON DELETE CASCADE

    );

     

     

    GO

    CREATE NONCLUSTERED INDEX [IX_OrderId]

        ON [dbo].[OrderDetails]([OrderId] ASC);

     

    I think the table datatypes match the datatypes of the OrderDetail Class definition. Why can’t LINQ make sense of the request for data? Any help would be greatly appreciated.

    Wednesday, February 14, 2018 5:14 PM

Answers

  • User283571144 posted

    Hi MPT79,

    According to your description and codes, I suggest you could try to modify the codes as below:

            Dim userLicenselist = (From c In pdb.OrderDetails Where c.Username = "aaa" AndAlso c.CategoryId = 5 Select c).ToList.FirstOrDefault
    

    Then it will work well.

    Result:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 15, 2018 7:02 AM

All replies

  • User283571144 posted

    Hi MPT79,

    According to your description and codes, I suggest you could try to modify the codes as below:

            Dim userLicenselist = (From c In pdb.OrderDetails Where c.Username = "aaa" AndAlso c.CategoryId = 5 Select c).ToList.FirstOrDefault
    

    Then it will work well.

    Result:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 15, 2018 7:02 AM
  • User759549661 posted

    Thanks, Brando, that did the trick!

    MPT79

    Thursday, February 15, 2018 4:25 PM