Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 有时在foreach循环中修改属性时,更改不会保留_C#_Linq - Fatal编程技术网

C# 有时在foreach循环中修改属性时,更改不会保留

C# 有时在foreach循环中修改属性时,更改不会保留,c#,linq,C#,Linq,有时,当循环通过LINQ返回的枚举并修改其中元素的属性时,更改在foreach循环之外丢失。到目前为止,我只在使用.Select()创建枚举时才注意到它,然后才在Select中创建新对象时注意到它 using System; using System.Collections.Generic; using System.Linq; namespace TestApp { class Program { static void Main(string[] args)

有时,当循环通过LINQ返回的枚举并修改其中元素的属性时,更改在foreach循环之外丢失。到目前为止,我只在使用.Select()创建枚举时才注意到它,然后才在Select中创建新对象时注意到它

using System;
using System.Collections.Generic;
using System.Linq;
namespace TestApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var collection1 = new List<TestClass>();
            collection1.Add(new TestClass() { Property = "initial" });
            collection1.Add(new TestClass() { Property = "initial" });
            var collection2 = new List<TestClass>();
            collection2.Add(new TestClass() { Property = "initial" });
            collection2.Add(new TestClass() { Property = "initial" });

            var enumerable1 = collection1.Select(x => x);

            var enumerable2 = collection2.Select(x => new TestClass() { Property = x.Property });

            foreach(var element in enumerable1)
            {
                element.Property = "new";
            }
            Console.WriteLine(enumerable1.First().Property); //outputs "new"

            foreach (var element in enumerable2)
            {
                element.Property = "new";
            }
            Console.WriteLine(enumerable2.First().Property); //outputs "initial"
        }
    }

    class TestClass
    {
        public string Property;
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
命名空间TestApp
{
班级计划
{
静态void Main(字符串[]参数)
{
var collection1=新列表();
collection1.Add(newtestclass(){Property=“initial”});
collection1.Add(newtestclass(){Property=“initial”});
var collection2=新列表();
collection2.Add(newtestclass(){Property=“initial”});
collection2.Add(newtestclass(){Property=“initial”});
var enumerable1=collection1.Select(x=>x);
var enumerable2=collection2.Select(x=>newtestclass(){Property=x.Property});
foreach(enumerable1中的var元素)
{
element.Property=“new”;
}
Console.WriteLine(enumerable1.First().Property);//输出“new”
foreach(enumerable2中的var元素)
{
element.Property=“new”;
}
Console.WriteLine(enumerable2.First().Property);//输出“initial”
}
}
类TestClass
{
公共字符串属性;
}
}

这是某种错误,还是我在做不该做的事?

发生的事情是:

foreach (var element in enumerable2)
{
    element.Property = "new";
}
您正在计算表达式:

collection2.Select(x => new TestClass() { Property = x.Property })
然后使用以下属性:
enumerable2.First().Property
,再次计算表达式。除非对表达式调用
ToList()
,否则将再次对表达式求值。您拥有的功能等同于:

foreach (var element in collection2.Select(x => new TestClass() { Property = x.Property }))
{
    element.Property = "new";
}
Console.WriteLine(collection2.Select(x => new TestClass() { Property = x.Property }).First().Property)
看看发生了什么?要解决此问题,请在
enumerable2的声明中调用
ToList()

var enumerable2 = collection2.Select(x => new TestClass() { Property = x.Property }).ToList()

这里发生的是:

foreach (var element in enumerable2)
{
    element.Property = "new";
}
您正在计算表达式:

collection2.Select(x => new TestClass() { Property = x.Property })
然后使用以下属性:
enumerable2.First().Property
,再次计算表达式。除非对表达式调用
ToList()
,否则将再次对表达式求值。您拥有的功能等同于:

foreach (var element in collection2.Select(x => new TestClass() { Property = x.Property }))
{
    element.Property = "new";
}
Console.WriteLine(collection2.Select(x => new TestClass() { Property = x.Property }).First().Property)
看看发生了什么?要解决此问题,请在
enumerable2的声明中调用
ToList()

var enumerable2 = collection2.Select(x => new TestClass() { Property = x.Property }).ToList()

为什么在使用enumerable1的第一个foreach循环中没有发生这种情况。使用第一个
foreach
循环时,您正在修改
collection1
中的元素,而使用第二个循环时,您正在修改在
Select
函数中创建的新元素,该新元素在每次计算时都会重新创建。为什么在使用enumerable1的第一个foreach循环中不会发生这种情况。在第一个
foreach
循环中,您正在修改
collection1
中的元素,而在第二个循环中,您正在修改在
Select
函数中创建的新元素,该新元素在每次对其求值时都会重新创建