List<PageInfo> subPages = new List<PageInfo>(); // ... // some code to populate subPages here... // ... List<Guid> subPageGuids = new List<Guid> {from x in subPages select x.Id}; //doesn't work
PageInfo has an Id field which is of type Guid. So x.Id is a System.Guid.
2nd line of code above does not work…I get errors:
The best overloaded Add method ‘System.Collections.Generic.List.Add(System.Guid)’ for the collection initializer has some invalid arguments
And
Argument ‘1’: cannot convert from ‘System.Collections.Generic.IEnumerable’ to ‘System.Guid’
I’ve only been coding in C# for about a week, but I’ve done a similar pattern before and never had this problem.
I think you want:
(note the curly braces changed to regular braces)
That will call the constructor of List that takes an IEnumerable (which is what the linq query returns) as a parameter. Right now you’re trying to use the syntax for an object initializer.