简体   繁体   中英

What's the difference between the two processes?

The first one using IEnumerable Single method and it has a InvalidOperationException.

RowObjectType = rowObjectAssemblyTypes.Single(type => type.Name == rowObjectTypeName);

I think the second one does the same thing as the first one and it works fine.

foreach (var type in rowObjectAssemblyTypes)
        {
            if (type.Name == rowObjectTypeName)
            {
                RowObjectType = type;
            }
        }

I'm using .Net3.5. Can anyone tell me the difference between them?

The first one probably crashes because there are zero items or more than one item. This is likely to be a bug and it is good that Single alerted you to that!

The loop doesn't care. It might never assign to RowObjectType or do it multiple times. That's semantically probably not what you want.

If you expect zero items use SingleOrDefault .

Well, technically seen these two do not work the same way.

RowObjectType = rowObjectAssemblyTypes.Single(type => type.Name == rowObjectTypeName);

Will return the only element in the enumerable. If there are either more than one or no elements, this method will throw an exception. A more solid way would be to use SingleOrDefault() and check for null .

The second;

foreach (var type in rowObjectAssemblyTypes)
    {
        if (type.Name == rowObjectTypeName)
        {
            RowObjectType = type;
        }
    }

Will always assign the last element of rowObjectAssemblyTypes which satisfies the if -statement to RowObjectType . If there is only one, it will be assigned properly. However, if there are more, the last object in the enumerable will be assigned.

If you only want the first one, consider;

foreach (var type in rowObjectAssemblyTypes)
    {
        if (type.Name == rowObjectTypeName)
        {
            RowObjectType = type;
            break;
        }
    }

Or, even better;

RowObjectType = rowObjectAssemblyTypes.FirstOrDefault(type => type.Name == rowObjectTypeName);

Single(...) requires that exactly one item satisfies the condition.
It will throw an exception if more than one item satisfies the condition.
https://msdn.microsoft.com/en-us/library/vstudio/bb535118%28v=vs.100%29.aspx
Perhaps you need First(...)

.single returns the only element of a sequence where type.Name == rowObjectTypeName , and throws an exception if there is not exactly one element in the sequence

While your second loop is assigning if applicable to more than one or none

The first one is using Linq to return a single object from the list, if there are more than one object which has the same name as rowObjectTypeName then it will throw an exception.

The second one does allows for multiple objects to contain the same name as rowObjectTypeName . However, the final result will be the last match.

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