The first compiles and runs . The second one fails because the method call returns an ICollection.
The following code works fine
foreach (XmlSchema schema in schemaSet.Schemas(targetNamespace))
{
Id = schema.Id;
Version = schema.Version;
}
Since I am going to to get only one schema, why not go ahead and do this
XmlSchema schema = schemaSet.Schemas(targetNamespace);
Id = schema.Id;
Version = schema.Version;
There has to be something which is similar to the second one which will work?
How can this be done?
You can use LINQ to make the second one work easily:
If you’re definitely expecting only a single schema, I would definitely opt for using
Single()instead ofFirst()– that way if your expectations are ever mistaken, you’ll throw an exception rather than using whichever schema happened to come first out of the unexpectedly-large collection.The
Cast<>()call is required becauseSchemas()only returns a weakly-typedICollectionrather than an implementation ofIEnumerable<XmlSchema>.