locked
Error filtering a related entity RRS feed

  • Question

  • I have a Client entity that can have zero or many Issues. The Client entity is from an external database and the Issues are on a local database. 

    In my PreprocessQuery method, trying to filter our all clients who do not have an issue. I have tried both Issues and IssuesQuery.

    query = query.Where(x => x.Issues.Count() > 0);

    I get an error and not sure what is wrong.

    An error occurred while running the screen. You can close the screen or ignore the error and continue using the screen. 
    Error details: Exception has been thrown by the target of an invocation.
    The expression is not supported.   Expression: LightSwitchApplication.Client1[].Where(x => (x.Issues.Count() > 0))
    Inner exception message:
    The expression is not supported.   Expression: x => (x.Issues.Count() > 0)
    Inner exception message:
    The expression is not supported.   Expression: x => (x.Issues.Count() > 0)
    Inner exception message:
    The expression is not supported.   Expression: (x.Issues.Count() > 0)
    Inner exception message:
    The expression is not supported.   Expression: x.Issues.Count()
    Inner exception message:
    The expression is not supported.   Expression: x.Issues
    Inner exception message:
    The member 'Issues' on type 'LightSwitchApplication.Client1' is not supported.



    Wednesday, April 9, 2014 9:56 PM

Answers

  • You cannot write a single expression that works against data from multiple data sources. You'll need to query the Issues entity set first and then use the results to limit the results in the PreprocessQuery. For example:

    partial void ClientsWithIssues_PreprocessQuery(ref IQueryable<Client> query)
    {
        IEnumerable<Issue> issues = this.DataWorkspace.ApplicationData.Issues.GetQuery().Execute();
        IEnumerable<int> clientsWithIssues = issues.Select(i => i.ClientID).Distinct().ToList();
    
        query = query.Where(c => clientsWithIssues.Contains(c.Id));
    }


    Justin Anderson, LightSwitch Development Team

    • Proposed as answer by babloo1436 Thursday, April 10, 2014 4:01 AM
    • Marked as answer by george1956 Thursday, April 10, 2014 1:55 PM
    Wednesday, April 9, 2014 11:34 PM
    Moderator