C#Linq查询无法正常工作

Die*_*ego 2 .net c# linq

我有一个Dictionary<MyEntity1, IList<MyEntity2>>我有一个MyEntity2的对象,我知道它在一个(只有一个)字典列表中(我甚至在调试时看到它).以下查询如何返回null:

IDictionary<MyEntity1, IList<MyEntity2>> myDictionary = new Dictionoary<MyEntity1, IList<MyEntity2>>();
MyEntity1 myEntity1 = (from p in myDictionary
                       where p.Value.Contains(myEntity2)
                       select p.Key) as MyEntity1;
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Joa*_*oao 6

var myEntity1 = (from p in myDictionary
                where p.Value.Contains(myEntity2)
                select p.Key).First();
Run Code Online (Sandbox Code Playgroud)

即使只有一个对象实例,LINQ查询也总是返回集合