How to convert from ICollection to IEnumerable?
13,638
Solution 1
Redefine your method as
private IEnumerable<string> GetDomains()
{
...
}
since you're wanting a list of string
and not Domains
or DirectoryEntry
. (assumed since you're adding "d.Name")
Also, it would be much easier to just use LINQ:
IEnumerable<string> domains = Forest.GetCurrentForest().Domains.Select(x => x.Name);
This will return an IEnumerable<string>
, and it won't waste extra memory creating a separate list.
Solution 2
Set the type of domains to IList<string>
or do as Nathan suggests:
private IEnumerable<string> GetDomains()
{
return Forest.GetCurrentForest().Domains.Select(x => x.Name);
}
Author by
user1144852
Updated on June 25, 2022Comments
-
user1144852 almost 2 years
In the below code the return statement is throwing exception.
private IEnumerable<DirectoryEntry> GetDomains() { ICollection<string> domains = new List<string>(); // Querying the current Forest for the domains within. foreach (Domain d in Forest.GetCurrentForest().Domains) { domains.Add(d.Name); } return domains; //doesn't work }
What could be the possible resolution to this issue?