2

我有Dictionary<Position, List<GameObject>>并且我想创建一个新的字典Dictionary<Position, List<Person>>来收集职位,其中至少包括一个人List<GameObject>

我试过:

Dictionary<Position, List<Person>> persons =
    positions.ToDictionary(
        i => i.Key, 
        i => i.Value.Where(obj => obj is Person))

positions是第一本词典。

但它并没有真正起作用,因为该位置有时为空,并且它不是真正的类型,Person因为您没有转换它。无论如何,你有什么想法吗?

4

1 回答 1

6
var people = positions.Select(x => new { x.Key, Values = x.Value.OfType<Person>().ToList() })
                      .Where(x => x.Values.Any())
                      .ToDictionary(x => x.Key, x => x.Values)
于 2013-07-11T08:01:04.090 回答