Alan2 Alan2 - 3 years ago 206
C# Question

How can I add a 0-9 and a 0-99 hash to a List

I have a variable that holds List

public class PhraseSource
public int CategoryId { get; set; }
[PrimaryKey, NotNull]
public string Id { get; set; }
public string English { get; set; }
public string Romaji { get; set; }
public string Kana { get; set; }
public string Kanji { get; set; }
public string WordType { get; set; }
public int Modified { get; set; }

I want to add two additional fields so I have added these:

public int OneHash { get; set; }
public int TwoHash { get; set; }

  • Where the value of OneHash is a hash that varies from 0-9 of the Id

  • Where the value of TwoHash is a hash that varies from 0-99 of the Id

Can someone give me some suggestions of how I can populate these two columns?

Answer Source

You can use

public int OneHash => Math.Abs(Id.GetHashCode() % 10);
public int TwoHash => Math.Abs(Id.GetHashCode() % 100);

It takes the Hashcode of the Id string and returns the reminder of an division.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download