开发者

Custom extension method for Linq To Entities

开发者 https://www.devze.com 2023-04-03 05:40 出处:网络
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)

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.开发者_StackOverflow

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消