这两个过程有什么区别?

时间:2022-01-03 15:48:48

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

第一个使用IEnumerable Single方法,它有一个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?

我正在使用.Net3.5。谁能告诉我他们之间的区别?

5 个解决方案

#1


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!

第一个可能崩溃,因为没有项目或多个项目。这很可能是一个错误,Single提醒你注意这一点!

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

循环并不关心。它可能永远不会分配给RowObjectType或多次执行。这在语义上可能不是你想要的。

If you expect zero items use SingleOrDefault.

如果您希望零项目使用SingleOrDefault。

#2


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.

将返回枚举中唯一的元素。如果有多个元素或没有元素,则此方法将引发异常。一种更可靠的方法是使用SingleOrDefault()并检查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语句的rowObjectAssemblyTypes的最后一个元素分配给RowObjectType。如果只有一个,它将被正确分配。但是,如果还有更多,则将分配枚举中的最后一个对象。

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);

#3


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(...)

单(...)要求恰好一个项满足条件。如果多个项满足条件,它将抛出异常。 https://msdn.microsoft.com/en-us/library/vstudio/bb535118%28v=vs.100%29.aspx也许你需要第一个(...)

#4


.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

.single返回type.Name == rowObjectTypeName的序列中唯一的元素,如果序列中没有一个元素则抛出异常

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

虽然你的第二个循环是分配,如果适用于多个或没有

#5


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.

第一个是使用Linq从列表中返回单个对象,如果有多个对象与rowObjectTypeName具有相同的名称,那么它将引发异常。

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

第二个允许多个对象包含与rowObjectTypeName相同的名称。但是,最终结果将是最后一场比赛。

#1


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!

第一个可能崩溃,因为没有项目或多个项目。这很可能是一个错误,Single提醒你注意这一点!

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

循环并不关心。它可能永远不会分配给RowObjectType或多次执行。这在语义上可能不是你想要的。

If you expect zero items use SingleOrDefault.

如果您希望零项目使用SingleOrDefault。

#2


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.

将返回枚举中唯一的元素。如果有多个元素或没有元素,则此方法将引发异常。一种更可靠的方法是使用SingleOrDefault()并检查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语句的rowObjectAssemblyTypes的最后一个元素分配给RowObjectType。如果只有一个,它将被正确分配。但是,如果还有更多,则将分配枚举中的最后一个对象。

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);

#3


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(...)

单(...)要求恰好一个项满足条件。如果多个项满足条件,它将抛出异常。 https://msdn.microsoft.com/en-us/library/vstudio/bb535118%28v=vs.100%29.aspx也许你需要第一个(...)

#4


.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

.single返回type.Name == rowObjectTypeName的序列中唯一的元素,如果序列中没有一个元素则抛出异常

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

虽然你的第二个循环是分配,如果适用于多个或没有

#5


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.

第一个是使用Linq从列表中返回单个对象,如果有多个对象与rowObjectTypeName具有相同的名称,那么它将引发异常。

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

第二个允许多个对象包含与rowObjectTypeName相同的名称。但是,最终结果将是最后一场比赛。