locked
Error in Entity Filter RRS feed

  • Question

  • I get an exception of type 'System.NotSupportedException' occurred in Microsoft.LightSwitch.dll but was not handled in user code,

    when trying to filter entities based on logged-in user as in this code:

     

            Private Sub Permits_Filter(ByRef filter As System.Linq.Expressions.Expression(Of System.Func(Of Permit, Boolean)))

                Dim emp As Employee = (From employees In Me.DataWorkspace.ApplicationData.Employees
                                       Where employees.UserName = Application.User.Name
                                       Select employees).FirstOrDefault()

                Dim vendorId As Integer = If(emp Is Nothing, -1, emp.Vendor.Id)

                If Not Me.Application.User.HasPermission(Permissions.CanViewAllPermits) Then
                    filter = Function(p) p.Vendor.Id = vendorId
                End If

            End Sub

    Why is this happening?

    Wednesday, October 15, 2014 12:56 AM

Answers

  • Hi Rubosh, I think linq itself can't directly access and resolve 'Application' object while building the query.

    Try storing the value in a simple string variable

    Dim uName As String = Application.User.Name
    Dim emp As Employee = (From employees In Me.DataWorkspace.ApplicationData.Employees
                           Where employees.UserName = uName Select employees).FirstOrDefault()

    or configure a new Employees query in your ApplicationData and pass Application.User.Name directly as a parameter

    Hope this helps


    Marco

    • Proposed as answer by fileman Wednesday, October 15, 2014 7:51 AM
    • Marked as answer by Isahak Wednesday, October 15, 2014 4:10 PM
    Wednesday, October 15, 2014 7:28 AM

All replies

  • Hi Rubosh, I think linq itself can't directly access and resolve 'Application' object while building the query.

    Try storing the value in a simple string variable

    Dim uName As String = Application.User.Name
    Dim emp As Employee = (From employees In Me.DataWorkspace.ApplicationData.Employees
                           Where employees.UserName = uName Select employees).FirstOrDefault()

    or configure a new Employees query in your ApplicationData and pass Application.User.Name directly as a parameter

    Hope this helps


    Marco

    • Proposed as answer by fileman Wednesday, October 15, 2014 7:51 AM
    • Marked as answer by Isahak Wednesday, October 15, 2014 4:10 PM
    Wednesday, October 15, 2014 7:28 AM
  • That sure did it! The error is gone and entities are filtered as intended.

    Thanks a lot for your help!


    Isahak

    Wednesday, October 15, 2014 4:11 PM
  • Found this at MSDN...which explains why the error was raised from linq query:

    LINQ Operations: Remote vs. Local Execution

    If you use an EntitySet or IDataServiceQueryable object in a LINQ expression, LightSwitch passes the entire LINQ expression to the data service of the server tier. 


    Isahak

    Wednesday, October 15, 2014 9:55 PM
  • The correct syntax for the _filter method is below.  The standard linq queries will work in the preprocess query method.

    From: http://msdn.microsoft.com/en-us/library/hh873169.aspx 

    The following code example filters the Employees entity so that the current user can display only the records that contain their Employee Name:

    partial void Employees_Filter(ref Expression<Func<Employee, bool>> filter)
            {
                  filter = e => e.EmployeeName == this.Application.User.Name;
            }

    vb:

    Private Sub Employees_Filter(ByRef filter As System.Linq.Expressions.Expression(Of System.Func(Of Employee, Boolean)))
       filter = Function(e) e.EmployeeName = Me.Application.User.Name
    End Sub

    Friday, October 17, 2014 3:07 AM