Question

in my lightswitch application im trying to create a Customers Screen for customers who have balance value, and my balance value is a Calculated Field in the Customer Entity

when i tried to put the logic in the Process Query Event Like This

query =( from i in query

where(i.Balance>0)
select i );

i get an exception .. what is the best way to handle these kind of situations ?? i saw an answer here but i didn't know how to implement it exactly i need a sample code for it can anyone help me ?? thanks in advance

Was it helpful?

Solution

The query will be executed by the data provider, which doesn't know about calculated fields. What you can do is to filter what you want via LINQ, referring to the actual fields, not the calculated ones.

For example, let's say Balance is your calculated field, that you defined as Credit - Debit (which are normal fields). You want your query to return the rows where Balance > 0. This is how you'd write the query (in the PreprocessQuery event, note there is no ProcessQuery event):

partial void TestQuery_PreprocessQuery(ref IQueryable<Customer> query)
{
    query = (
        from c in query
        where ((c.Credit - c.Debit) > 0)
        select c);
}

Another theoretical way of solving the problem would be setting a filter in the Executed event handler. However, for whatever the reason, when I do it, the filter is not applied to the screen. But even if this method would work, still you'd be filtering on the client side, which might not be what you want.

OTHER TIPS

I'm actually looking for a solution to this problem (unfortunately I can't just include the calculation from the calculated field, because my calculation uses another calculated field which uses recursion)

A couple of pointers I thought you might find helpful: @julio.g - for the Executed event handler, the "result" parameter is an IEnumerable parameter (as opposed to the PreProcess_Query event handler, where the "query" parameter is a ref IEnumerable) so any changes you make to the "result" will only be local to that method

@3oon - afaik, SQL Views are not supported in LightSwitch. The best option I've come across so far is to create a WCF RIA Service based on a Stored Procedure, then adding it as a datasource. This blog post should help get you started. http://tejana.wordpress.com/2010/12/09/microsoft-lightswitch-and-stored-procedures-using-wcf-ria-services/

Hope that helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top