2

When the DoTranslation method is run, the output is the same hash code for all of the TranslatedObjects. Why is this happening as opposed to having a new List for each TranslatedObject?

public class TranslatedObject
{
    public static Expression<Func<OriginalObject, TranslatedObject>> ObjectTranslator = o => new TranslatedObject
    {
        id = o.id
        translatedList = new List<String>()
    };

    public int id { get; set; }
    public List<String> translatedList { get; set; }
}


public class Translator
{
    public void DoTranslation()
    {
        //3 objects returned with ids 1, 2, and 3 respectively
        IQueryable<OriginalObject> originalObjects = dataContext.OriginalObjects.Where(o => o.id == 1 || o.id == 2 || o.id == 3);

        var translatedObjects = originalObjects.Select(TranslatedObject.ObjectTranslator).ToArray();

        foreach(TranslatedObject translated in translatedObjects)
        {
            Console.WriteLine(translated.translatedList.GetHashCode());
        }
    }
}

UPDATE: Changed service call to the following linq-to-sql call: dataContext.OriginalObjects.Where(o => o.id == 1 || o.id == 2 || o.id == 3).

zc22
  • 161
  • 1
  • 8
  • What do the translated lists contain? All of them are equal? – Davide Lettieri Jul 12 '13 at 15:06
  • 2
    I get the expected result (different HashCodes): http://ideone.com/nWfjap – Cristian Lupascu Jul 12 '13 at 15:08
  • It returns different results for me too. Tried all versions of .NET and Debug/Release. – Euphoric Jul 12 '13 at 15:08
  • Possibly relevant: http://stackoverflow.com/questions/8178115/ – valverij Jul 12 '13 at 15:09
  • Every translatedList is just an empty List right. So what is wrong with them returning the same hashcode?? It doesn't break any definitions.....and in fact looks pretty natural - they're 3 equivalent(not equal) collections of the same type..... What makes you think it's the same list being re-used?? 2 objects can have same hashcode without being equal. – Vivek Jul 12 '13 at 15:45
  • Thanks for the quick responses. I ran the code snippet from w0lf and it did write different hash codes. The service.GetOriginalObjects() call is actually a linq-to-sql query that returns an IQueryable. Something along the lines of dataContext.OriginalObjects.Where(o => o.id == 1 || o.id == 2 || o.id == 3). Do you see any reason why this would cause the hash codes to be the same? – zc22 Jul 12 '13 at 17:11
  • 2
    I just tried your code, where I made service a Linq-to-SQL DataContext. And yes, I can confirm that all translatedList in different TranslatedObjects are same instance of that one list. So this is problem related to that and you should update your question accordingly. – Euphoric Jul 12 '13 at 18:07
  • @Vivek: Constructing two distinct `List` objects and leaving them empty will produce different hash codes since `List` doesn't care about the contents, it only does reference equality. – Lasse V. Karlsen Jul 12 '13 at 23:04
  • @Lasse: That may be so. The point i was raising was that the 2 lists having the same hash code is not wrong.... it's very much within the definition, and concluding that 2 objects are the same if their hashes match is what's wrong. It just so happens List just uses the default object.GetHashCode(afaik) which generates a fresh int for every new object. – Vivek Jul 15 '13 at 14:32

0 Answers0