0

We are using string.GetHashCode() to identify a unique string identifier as an integer in our application (in a location where the ID is required to be an Integer).

Since this is a key field for a particular table in our database I need to be able to reference it from time to time in a LINQ query.

Of course, this fails with the error:

LINQ to entities does not recognize the method 'blah'

We get around that in a few places by post processing the query and adding the relevant data to the collection after the query... but in one particular query the results are just too large and the post processing is just not going to work.

So, my question is simple, but maybe not so simple to answer: Is there a best way to incorporate the use of string.GetHashCode() inside a linq query?

I have tried to create a Dictionary<string, int>(), but LINQ does not like querying that object either :/

Any help appreciated!

Dave

Stefan Wuebbe
  • 2,109
  • 5
  • 17
  • 28
  • 1
    How could this be translated to SQL? https://stackoverflow.com/questions/15174477/does-string-gethashcode-consider-the-full-string-or-only-part-of-it Why do you need that in the database? Filter as much as possible and then append AsEnumerable. After this you can use GetHashCode if you really need it. So it happens in memory. – Tim Schmelter Apr 20 '23 at 23:02
  • 5
    _"using string.GetHashCode() to identify a unique string identifier as an integer in our application"_ - in general case you should not - from the [docs](https://learn.microsoft.com/en-us/dotnet/api/system.string.gethashcode): _"The hash code itself is not guaranteed to be stable. Hash codes for identical strings can differ across .NET implementations, .NET versions and .NET platforms (such as 32 or 64-bit) for a single version of .NET. In some cases, they can even differ by application domain. This implies that two subsequent runs of the same program may return different hash codes."_ – Guru Stron Apr 20 '23 at 23:10
  • Could you use a hash function like SHA256 instead, and just take the first 32 bits for your integer ID? MS SQL appears to have common ones implemented in [HASHBYTES](https://learn.microsoft.com/en-us/sql/t-sql/functions/hashbytes-transact-sql?view=sql-server-ver16). so you could possibly create a stored procedure and call that in your query. – IllusiveBrian Apr 20 '23 at 23:44
  • This sounds like a better job for an enum (which is effectively the simplest kind of minimal perfect hash). An arbitrary hash function will *not* guarantee your strings map to unique values, and with a 32-bit value, the chance may be [bigger than you think](https://preshing.com/20110504/hash-collision-probabilities/). If you have a small number of strings, the chance of a collision is small, but then an enum would be even more appropriate. – Jeroen Mostert Apr 21 '23 at 09:44
  • Are you attempting to use `GetHashCode()` on data from the table or on data in your application that is used to narrow the query? If the latter, you can just assign to a local variable and reference in the query. – NetMage Apr 21 '23 at 19:31

0 Answers0