HectorLector
HectorLector

Reputation: 1911

lightswitch LINQ PreprocessQuery

I use the PreprocessQuery method to extend a query in lightswitch. Something like this:

query = (from item in query where (validIDs.Contains(item.tableIDs.myID)) && elementCount[item.ID] <= maxEleCount) select item);

Where validIDs is a HashSet(int) and elementCount is a Dictionary(int, int). the first where clause is working fine, but the second -> elementCount[item.ID] <= maxEleCount is not working.

What i want to do is to filter a table by some IDs (validIDs) and check also if in another table the number of entries for every of this IDs does not exceed a limit.

Any ideas?

EDIT I found a solution. Instead of a Dictionary I also used a HashSet for the second where clause. It seems it is not possible to do the Dictionary lookup inside the LINQ statement for some reason (?)

Upvotes: 1

Views: 1792

Answers (1)

Yann Duran
Yann Duran

Reputation: 3879

First, although being a bit pedantic, what you're doing in a PreProcessQuery method is "restricting" records in the query, not "extending" the query.

What you put in a LING query has to be able to be processed by the Entity Framework data provider (in the case of LS, the SQL Server Data Provider).

Sometimes you'll find that while your LINQ query compiles, it fails at runtime. This is because the data provider is unable to express it to the data store (again in this case SQL Server).

You're normally restricted to "primitive" values, so if you hadn't said that using a Dictionary actually worked, I would have said that it wouldn't.

Any time you have a static (as in non-changing) value, I'd suggest that you create a variable outside of your LINQ query, then use the variable in the LINQ query. By doing this, you're simply passing a value, the data provider doesn't have to try to figure out how to pass it to the data store.

Reading your code again, this might not be what you're doing, but hopefully this explanation will still be helpful.

Upvotes: 2

Related Questions