2

I have Dictionary<Position, List<GameObject>> and I want to create new dictionary Dictionary<Position, List<Person>> which collect positions which including at least one person in the List<GameObject>.

I tried:

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

positions is the first dictionary.

but it doesn't really work because the position sometimes is null and its not really type of Person because you don't convert it. anyway, do you have any ideas?

1 Answer 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) 
Sign up to request clarification or add additional context in comments.

1 Comment

the plural of person is people, other than that +1

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.