Search code examples
c#htmlvisual-studio-lightswitch

Log a query that returns no results in Lightswitch HTML


What i would like to do is, when a user does a search and the query doesn't return a result. I want to be able to log the string that was searched in another table.

What I'm trying to do seems like it should be straight forward... My first approach was to get the count of a PreprocessQuery.

Example of what i tried:

    int count = 0;
    partial void QueryName_PreprocessQuery(string ParamName, ref IQueryable<TableName> query)
        {
            if (ParamName != null)
            {
                query = query.Where(a => a.PropertyName.Contains(ParamName));
                count = query.Count();
            }
        }

When i place a breakpoint and step through this method. count is always 0 and query is always empty. The odd thing is, this query still brings back the correct results. I've tried various different approaches and even started a new Lightswitch project with a single screen and a single query. Same results.

Another example of what i have tried:

partial void QueryName_PreprocessQuery(string ParamName, ref IQueryable<TableName> query)
            {
                if (ParamName != null)
                {
                    count = query.Count(a => a.Serial_No.Contains(SerialNumber));                    
                }
            }

This method doesn't actually do anything except validate that query is empty and count is 0. And again... the correct results still show up on my screen.

So this leads me to believe that maybe i shouldn't be trying to get the count in the _PreProcessQuery Method?


Solution

  • I believe you're right. Try changing from QueryName_PreprocessQuery to QueryName_Executed instead.

    QueryName_PreprocessQuery occurs when the query is being formed and prior to execution.

    QueryName_Executed is called just after the query is executed and thus should give you the information you're trying to obtain.

    You could try something like this:

    partial void QueryName_Executed(QueryExecutedDescriptor queryDescriptor)
        {
            int resultCount = queryDescriptor.Results.Count();
        }