Question

I'd like to make a query to my SQL Server 2008 R2 database using Linq-To-Entities.
I use Entity Framework 4.1 (Code-First)
the query has to compare the entities' hashed values with a paramater as follows:

myContext.Messages.Where(m => HashCode(m.MessageId) % 10000 == myValue)  

But I can only use core extension methods and CLR methods defined for the EF provider.
So I'm trying to translate my HashCode() method to something that L2E understands just before execution.

FYI, HashCode's implementation is not complex. something like:

public int HashString(string text)
{
    int hash = 23;
    foreach (char c in text)
    {
        hash = hash * 31 + c;
    }
    return hash;
}

I know I can retrieve all the entities as enumerable and check them in my code (Linq To Objects), But that's be a disaster considering the amount of data I'm about to retrieve that way.

Is it doable at all (for GetHashCode method). If so, how can I implement it?
Thank you.

Was it helpful?

Solution

Could you write SQL to do this? I don't see how (without a SQL function, which requires modifying DB schema, which you say you can't do).

If you can't do it in SQL, you certainly can't do it in L2E, since all L2E is translated to SQL.

On the other hand, if you can do it in SQL, the fastest solution is probably ObjectContext.ExecuteStoreQuery.

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