是否有一种简单的方法基本上只获取数据的副本而不是使用此方法的引用?我尝试了 .ToArray().Where() 但它似乎仍然传递了一个引用。
例子:
static void Main(string[] args)
{
List<ob> t = new List<ob>();
t.Add(new ob() { name = "hello" });
t.Add(new ob() { name = "test" });
ob item = t.Where(c => c.name == "hello").First();
// Changing the name of the item changes the original item in the list<>
item.name = "burp";
foreach (ob i in t)
{
Console.WriteLine(i.name);
}
Console.ReadLine();
}
public class ob
{
public string name;
}
最佳答案
您需要自己创建 ob
的副本 - LINQ 不提供此功能。
关于c# - 如何在 C# 中使用 linq/lambda 获取数据副本而不是引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8724421/