locked
Unable to use LINQ properly in Pre Process Query (Select throws a wobbler!) RRS feed

  • Question

  • Either I'm doing it wrong, or this is a rather silly limitation....

    In a Table Query, PreporcessQuery part, I CANT do the following:

     

      Private Sub VisibleIncidentReports_PreprocessQuery(ByRef query As System.Linq.IQueryable(Of LightSwitchApplication.IncidentReport))
    
       'We only want the incident reports for the entities a user is actually allowed to see. The linking table is UserEntities
       'This table links Username to EntityId.
       'An incident report always belongs to an EntityId
    
       Dim allIds2 = (From ue In UserEntities Where ue.UserName = Application.Current.User.Name Select ue.EntityId).Execute().ToList
    
       query = From ir In query
         Where allIds2.Contains(ir.Entityy.Id)
         Select ir
      End Sub
    

    We get the error: {"Selecting the source element is the only supported selector.

     

    Parameter name: selector"} on the first linq statment.

     

    Instead, I have to do this:

     

     

      Private Sub VisibleIncidentReports_PreprocessQuery(ByRef query As System.Linq.IQueryable(Of LightSwitchApplication.IncidentReport))
    
       'We only want the incident reports for the entities a user is actually allowed to see. The linking table is UserEntities
       'This table links Username to EntityId.
       'An incident report always belongs to an EntityId
    
       'Must avoid using the SELECT keyword because it all breaks, even though it's nothing to do with our query.
       Dim allIds = (From ue In UserEntities Where ue.UserName = Application.Current.User.Name).Execute()
    
       'Jump through hoops rather than using LINQ to do this
       Dim allIds2 As New List(Of Integer)
       For Each link In allIds
        allIds2.Add(link.EntityId)
       Next
    
       query = From ir In query
         Where allIds2.Contains(ir.Entityy.Id)
         Select ir
      End Sub
    

     


    and it works as expected, we have restricted what the user can 'see' by linking it to the joining table and using a Contains... which we had to use Execute to obtain, but don't go there.

     

    So, I understand why I can't just go round selecting whatever I want in the ByRef Query, because you only want the full table back, that's fine... but that means I can use Select for ANYTHING while I figure out what rows I need?

    is that right?

     

    PS, the reason for Entityy is simple, you can't have a table called Entity in LightSwitch, it goes bang!


    Wednesday, August 3, 2011 2:14 PM

All replies

  • Hi

    It is not possible to select fields in a query Lightswitch, but if the list of controls within each screen.

    Jaime

    Wednesday, August 3, 2011 3:35 PM
  • Hi,

    thanks for your post. I think it could help me for what I want to do.

    Could you classify the relationships between all relevant tables in your example?

    Thanks in advance.

    Tuesday, October 18, 2011 1:12 PM
  • Something like this will select all the incident reports the have a related UserName that matches current User.Name. This assumes Users Tables has a UserName string field.

    partial void VisibleIncidentReport_PreprocessQuery(ref IQueryable<IncidentReport> query)
    {
        string cUser = Application.User.Name;
        query = from vi in query
                where vi.User.UserName == cUser
                select vi;
    } 


    - "Five is right out!" Please mark as answered if this answers the question :-)
    Tuesday, October 18, 2011 4:52 PM
  • You can simplify the code by doing this, instead of jumping thru hoops (C#):

    //Must avoid using the SELECT keyword because it all breaks, even though it's nothing to do with our query
       List<int> allIds = (From ue In UserEntities 
                           Where ue.UserName = Application.Current.User.Name 
                           Select ue.EntityId)
                           .Execute()
                           .ToList();
    
       query = From ir In query
               Where allIds.Contains(ir.Entity.Id)
               Select ir

    Tuesday, March 25, 2014 6:45 PM