我希望从枚举中获取枚举的子集,以便稍后根据Dictionary的值选择随机枚举元素。我试过这样做:
var allEnums = Enum.GetValues(typeof(MyEnum)).Cast<MyEnum>();
var availableEnums = allEnums.Where(e => SomeClass.SomeDictionary[e] < someThreshold);
//Note: SomeDictionary<MyEnum, int>
此外,这也不起作用:
var availableEnums = SomeClass.SomeDictionary.Where(entry => entry.Value < someThreshold);
availableEnums的值始终为{System.Linq.Enumerable + c__Iterator1D`1 [[AircraftModel,Assembly-CSharp,Version = 0.0.0.0,Culture = neutral,PublicKeyToken = null]]}。我必须做一些非常愚蠢的事情,但我怎么做到这一点呢?
答案 0 :(得分:0)
你想用
var availableEnums = SomeClass.SomeDictionary
.Where(entry => entry.Value < someThreshold)
.ToList();
// or
var availableEnums = SomeClass.SomeDictionary
.Where(entry => entry.Value < someThreshold)
.ToArray()
FindAll在Linq扩展和项目结果之前立即生效,而Where is deferred,它允许您在投射结果之前有效地链接多个操作,如OrderBy,Take等。