迭代器变量在它来自的 IEnumerable 中不存在?

Iterator variable doesn't exist in IEnumerable from which it came?

我有一个方法看起来像

private Component[] AssociateComponentsWithParametersAndValues(
            IEnumerable<Component> components,
            IEnumerable<ComponentParameter> parameters,
            IEnumerable<ComponentParameterValue> values
            )
        {
            var componentsDictionary = new Dictionary<string, Component>();
            var parametersDictionary = new Dictionary<string, ComponentParameter>();
            var valuesDictionary = new Dictionary<string, ComponentParameterValue>();
            foreach (Component c in components)
            {
                bool componentMatch = components.Any(co => co == c);
                bool identifierMatch = components.Any(co => co.Identifier == c.Identifier);
                if (!componentsDictionary.ContainsKey(c.Identifier))
                    componentsDictionary.Add(c.Identifier, c);
            }
            // Do a bunch of stuff to mutate the components
            return components.ToArray();
        }

您会认为 componentMatchidentifierMatch 每次都是正确的,对吗?相反,componentMatch 始终为假,而 identifierMatch 始终为真。此外,标识符(几乎,偶尔会有一些坏数据)总是唯一的,所以它不像是可以找到另一个具有相同标识符的组件。

所以,组件 class 一定有什么奇怪的地方。好吧,这是它的样子

public class Component : ConfigurationObject
    {
        public string Parent { get; set; }
        public string Type { get; set; }
        public string Module { get; set; }
        public string Code { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string TypeName { get; set; }
        public bool? Enabled { get; set; }
        public string DBIdentifier { get; set; }
        public Dictionary<string, ComponentParameterAndValues> ParametersAndValues { get; set; }
        public override string Identifier => DBIdentifier;
    }

这是它实现的class

public abstract class ConfigurationObject
    {
        public abstract string Identifier { get; }
    }

为什么会这样?

我能看到这个中断的唯一方法是,如果 IEnumerable<Component> components 是一个延迟计算的可枚举对象,每次都返回新的迭代器对象。这有效:

var list = new List<Component>
{
    new Component { Identifier = "Foo" },
    new Component { Identifier = "Bar" },
    new Component { Identifier = "Baz" },
};

foreach (Component c in list)
{
    bool componentMatch = list.Any(co => co == c);
    Console.WriteLine($"Component {c.Identifier} match: {componentMatch}");
}

因为 == 检查引用相等性(除非 Component 覆盖它,但它看起来不像)。但是,如果它不是列表,而是每次迭代都有一个新结果:

IEnumerable<Component> list = GetList();

foreach (Component c in list)
{
    bool componentMatch = list.Any(co => co == c);
    Console.WriteLine($"Component {c.Identifier} match: {componentMatch}");
}

private static IEnumerable<Component> GetList()
{
    yield return new Component { Identifier = "Foo" };
    yield return new Component { Identifier = "Bar" };
    yield return new Component { Identifier = "Baz" };
}

然后它打印 false,因为 foreach()Any() 各自获得新对象的新集合,所以它们的引用不匹配。

解决方案是枚举一次,存储组件一次,具体化在列表中,然后使用:

var localComponents = components.ToList();

foreach (Component c in localComponents)
{
    // ...
}