I was able to create a LINQ statement that I thought was strange and wanted to see if anyone else had experience with it.
I’ve simplified it to this:
var x = db.Test
.Where(a => a.Field1 == Utils.CreateHash(Preferences.getValue(a.Field2)))
.FirstOrDefault();
Now how does this translate to database code? Wouldn’t LINQ need to do a double query for every single row, i.e. for row a:
1) Query a.Field2 2) Return value to run Utils.CreateHash(Preferences.getValue(a.Field2)) 3) Take that value from step 2 and compare it against a.Field1 4) Repeat 1-3 until I've gone through all the rows or returned a matching row
Wouldn’t this be extremely inefficient? Or is LINQ smart enough to run this in a better way? Note, I haven’t actually run this code so another possibility is a runtime error. Why wouldn’t LINQ be smart enough to detect a conflict then and not let me compile it?
The query as is will not work since have a call to
Utils.CreateHashin your lambda that you are trying to execute on the DB – in that context you cannot execute that method since there simply is no equivalent on the DB side hence the query will fail.In general the ability of 3rd party Linq
IQuerableproviders (e.g. Linq to SQL, Linq to Entities) to access in memory constructs such as methods or classes is very limited, as a rule of thumb at most accessing primitive values or collections of primitives will work.