2

运行 DoTranslation 方法时,输出是所有 TranslatedObjects 的相同哈希码。为什么会发生这种情况,而不是为每个 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());
        }
    }
}

更新:将服务调用更改为以下 linq-to-sql 调用:dataContext.OriginalObjects.Where(o => o.id == 1 || o.id == 2 || o.id == 3)。

4

0 回答 0