I am trying to convert a Linq query that I have working in Linq to be able to work in dynamic linq (using System.Linq.Dynamic) because I want a user to be able to form their own queries and this query string would be added onto other query strings at runtime.
I have a query:
db.incidents.Where(a => a.incidentLocations.Single().location.street.Contains(location);
and I have tried to convert it to the following dynamic linq string:
query =
string.Concat("incidentLocations.Single().location.street.Contains(\"", location, "\")");
db.incidents.Where(query);
Where location is a string that includes search text.
I have managed to convert all my other queries to dynamic linq but this one i am struggling with the exception error:
“No applicable aggregate method ‘Single’ exists”
I understand that dynamic linq does not support all extension methods, could someone possibly tell me how I could get round this problem.
Get the source of Linq.Dynamic, copy paste the Where method, change the signature and the string with the function name inside the method and you’re good to go. I did it to add Single First etc, I can’t copy it here because I’m not on my dev machine but I’ll do it later if necessary 😉
EDIT: here’s the Single method if you decide to use it: