1

Possible Duplicate:
Good GetHashCode() override for List of Foo objects

I have List of Objects, Object has ID, FileName, Status(Enumerator), DateStamp, UserId.
Using Linq I filter List using UserId and send it to Web App.

I need to get some kind of Unique Id for filtered result and compare it next time to detect changes for current user. If new Object added for current user or property of existing object is changed Unique Id should be different.

I have tried to use code below to get Hash Code but hash code is different for every myList object even objects and object properties are the same. Any ideas please?

myList= _queueList.GetItems(p => p.User.Id == userId)
                  .OrderByDescending(p => p.DateStamp)
                  .ToList();

myList.GetHashCode()

var uniqueId = string.Join(",", myList.Select(x=>x.Id.ToString()).ToArray());

If the string is very long, you could generate an md5 or sha1 and return that instead.

4

2 に答える 2

1
var uniqueId = string.Join(",", myList.Select(x=>x.Id.ToString()).ToArray());

文字列が非常に長い場合は、md5またはsha1を生成して、代わりにそれを返すことができます。

于 2012-08-07T13:48:39.730 に答える
1

You could leverage builtin cryptography like this:

string hash = Convert.ToBase64String(
    new SHA1CryptoServiceProvider().ComputeHash(
        Encoding.UTF8.GetBytes(
            string.Join(":", myList.Select(obj => obj.Id.ToString()).ToArray()
        )));

I put Convert.ToBase64String() to make it web-friendly (beware: the resulting string might become quite long if you have many records!)

于 2012-08-07T13:51:42.270 に答える