简体   繁体   中英

Why does one code snippet compile, but the other does not?

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?

Since Schemas() returns an ICollection , you can't convert that to a single XmlSchema element. To get the single element from the collection, you need:

var schema = schemaSet.Schemas(targetNamespace).Cast<XmlSchema>().First();
Id = schema.Id;
Version = schema.Version;

(This uses LINQ's IEnumerable.Cast<TResult> to turn the non-generic collection into a generic one so you obtain a result of the correct type XmlSchema , so you may have to add a using directive for System.Linq .)

You can use LINQ to make the second one work easily:

XmlSchema schema = schemaSet.Schemas(targetNamespace).Cast<XmlSchema>().Single();
Id = schema.Id;
Version = schema.Version;

If you're definitely expecting only a single schema, I would definitely opt for using Single() instead of First() - 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 because Schemas() only returns a weakly-typed ICollection rather than an implementation of IEnumerable<XmlSchema> .

You can do:

XmlSchema schema = schemaSet.Schemas(targetNamespace).Cast<XmlSchema>().Single();
Id = schema.Id;
Version = schema.Version;

The .Single() will throw an exception if .Schemas() returns 0 items though.

You could also use .SingleOrDefault() to ensure that there are no more than 1, but just return null if there are 0:

XmlSchema schema = schemaSet.Schemas(targetNamespace).Cast<XmlSchema>().SingleOrDefault();
if(schema != null)
{
  Id = schema.Id;
  Version = schema.Version;
}

schemaSet.Schemas(targetNamespace) is an IEnumerable you cant assign it to XmlSchema .

Try to assign it to IEnumerable<XmlSchema>

Afterwards you have to select a defined one. See other answers.

Please don't ignore XmlSchemas by selecting any.

Ignoring Information often is the wrong way. There should be a qualified way to find the desired XMLSchema.

In my opinion First() isn't.

Error is this:

XmlSchema schema = schemaSet.Schemas(targetNamespace);

You are trying to return a list of schemas in a single schema object. This will not work.

You can either do:

IList<XmlSchema> schema = schemaSet.Schemas(targetNamespace);

and loop.

or this to return a specific schema based upon index.

XmlSchema schema = (from s in set.Schemas().Cast<XmlSchema>().ToList()
                            select s).FirstOrDefault();

The two pieces of code are not equivalent. The first piece of code iterates over the collection and sets the values to the last item in the collection.

To get the same behaviour you need to do a FirstOrDefault() or SingleOrDefault() to protect where the .Schemas method returns no elements.

XmlSchema schema = schemaSet.Schemas(targetNamespace).FirstOrDefault();
if (schema != null)
{
        Id = schema.Id;
        Version = schema.Version;
}

EDIT: Noting Jon Skeet's answer add

as XmlSchema

to the end or do the cast as he mentioned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM