locked
Join In PreprocessQuery RRS feed

  • Question

  • Can someone give an example of how to do a Join in a PreprocessQuery?  How do you reference entitiy collections other than the IQueryable<Entityx> passed to the method?
    Sunday, August 29, 2010 11:36 AM

Answers

  • If you want to filter or sort across to a related entity type, you can just use the navigation property in the Where/OrderBy condition.  For example, say I have an Order -> Customer relationship, I could write:

    Private Sub Query1_PreprocessQuery(ByRef query As System.Linq.IQueryable(Of Application10.Order))
    query = query.Where(Function(o) o.Customer.City = "Fargo")
    query = query.OrderBy(Function(o) o.Customer.City)
    

    If you want to go the other way, it is possible through the Any and All operators:

    Private Sub Query2_PreprocessQuery(ByRef query As System.Linq.IQueryable(Of Application10.Customer))
      query = query.Where(Function(c) c.Orders.Any(Function(o) o.OrderDate = Date.Today))
    End Sub
    
    
    If you want to select both Orders its corresponding Customer, this is currently not possible in LightSwitch through the PreprocessQuery.
    Monday, August 30, 2010 5:38 PM

All replies

  • If you want to filter or sort across to a related entity type, you can just use the navigation property in the Where/OrderBy condition.  For example, say I have an Order -> Customer relationship, I could write:

    Private Sub Query1_PreprocessQuery(ByRef query As System.Linq.IQueryable(Of Application10.Order))
    query = query.Where(Function(o) o.Customer.City = "Fargo")
    query = query.OrderBy(Function(o) o.Customer.City)
    

    If you want to go the other way, it is possible through the Any and All operators:

    Private Sub Query2_PreprocessQuery(ByRef query As System.Linq.IQueryable(Of Application10.Customer))
      query = query.Where(Function(c) c.Orders.Any(Function(o) o.OrderDate = Date.Today))
    End Sub
    
    
    If you want to select both Orders its corresponding Customer, this is currently not possible in LightSwitch through the PreprocessQuery.
    Monday, August 30, 2010 5:38 PM
  • Thank you Eric. Is there a way to place this code in a Server side UserCode class, so I can apply it to multiple Search screens?
    Artem Loshak PCLP
    Monday, March 7, 2011 1:49 AM
  • If it's reusability that you're after, could you not just create a single query which includes the PreprocessQuery code and reuse it across your multiple search screens?
    Monday, March 7, 2011 10:11 AM