- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我知道 IEnumerable.ToList() 应该创建一个新列表,但项目指向 IEnumerable 中相同的原始项目,如 ToList()-- Does it Create a New List? 中所述。
但是,我使用 VS 2012 的代码出现了一些奇怪的行为;工作框架;和.NET 4.0。当 IEnumerable.SequenceEquals() 似乎没有按我预期的那样工作时,它就开始了。我仔细研究了我的 QuickWatch 对话框,令人难以置信的是,以下语句的计算结果为 false:
this.Items.First () == this.Items.ToList ()[ 0 ]
我什至试过:
this.Items.ToList ().IndexOf(this.Items.First ())
评估为 -1。
Items
被声明为 WPF 自定义控件的属性,如下所示:
public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register (
"Items",
typeof ( IEnumerable<UserLayoutType> ),
typeof ( UserLayoutSelectorControl ),
new FrameworkPropertyMetadata ( null, FrameworkPropertyMetadataOptions.AffectsRender, UserLayoutSelectorControl.PropertyChanged ) );
public IEnumerable<UserLayoutType> Items
{
get
{
return ( IEnumerable<UserLayoutType> ) this.GetValue ( UserLayoutSelectorControl.ItemsProperty );
}
set
{
this.SetValue ( UserLayoutSelectorControl.ItemsProperty, value );
}
}
UserLayoutType 只是一个由 XSD 工具生成的类,声明如下:
//
// This source code was auto-generated by xsd, Version=4.0.30319.17929.
//
namespace MyAssays.UserLayoutCore.UserLayoutUtility {
using System.Xml.Serialization;
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute("UserLayout", Namespace="", IsNullable=false)]
public partial class UserLayoutType {
这是工厂类中首先创建 UserLayoutType 项的方法:
public static IEnumerable<UserLayoutType> CreateFromFolder ( string folderPath )
{
if (String.IsNullOrEmpty(folderPath))
throw new ArgumentNullException("folderPath", "Folder path must not be null");
var userLayoutXmlFilePaths = Directory.GetFiles ( folderPath ).Where ( filePath => filePath.EndsWith ( ".UserLayout.xml", StringComparison.InvariantCultureIgnoreCase ) );
return userLayoutXmlFilePaths.Select(filePath => UserLayoutFactory.CreateFromFile(filePath));
}
public static UserLayoutType CreateFromFile ( string filePath )
{
using ( var stream = new StreamReader ( filePath ) )
{
return ( UserLayoutType ) new XmlSerializer ( typeof ( UserLayoutType ) ).Deserialize ( stream );
}
}
有人知道发生了什么事吗?见下图:
最佳答案
您从中看到新对象的主要原因可能是 IEnumerable<T>
正在包装一个生成器,而不是一个具体化的集合。
这是一个简单的 LINQPad程序演示:
void Main()
{
IEnumerable<string> collection =
from index in Enumerable.Range(1, 10)
select "Index=" + index;
var list1 = collection.ToList();
var list2 = collection.ToList();
ReferenceEquals(list1[0], list2[0]).Dump();
}
这将打印 False
.
之所以这样做,是因为枚举集合的行为(在本例中为 .ToList()
)将执行延迟的 LINQ 查询,并且由于我们枚举集合两次,所以我们执行两次,产生不同的实例相同的值。
关于c# - 奇怪,IEnumerable.ToList() 创建了全新的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18436037/
在 ToList() 中指定具体类型是否有意义? , AsEnumerable()等方法? 威尔.ToList()执行速度比 .ToList() 快? 最佳答案 如果编译器推断出一种类型而您想要指定另
给定在内存中(不是 LINQ to SQL)类列表: List myItems = /*lots and lots of items*/; 我正在使用 GroupBy() 语句对其进行分组: myIt
我对此不是 100%,所以我需要专家的意见。 ConcurrentQueue queue = new ConcurrentQueue(); List listA = queue.ToArray().T
JDK 引入了一个 API Stream.toList() 和 JDK-8180352 。这是我尝试将其性能与现有 Collectors.toList 进行比较的基准测试代码: @BenchmarkM
说到性能,我应该使用 .ToList().Distinct() 还是 .Distinct().ToList() ? 两种扩展方法是否生成相同的 SQL 查询? 看起来第二种方法应该表现更好,但这是真的
当我调用它时: using (var db = new MyDbContext()) { var yy = db.Products.Select(xx => xx.Id); var i
我正在寻找这些 LINQ 表达式的确认/澄清: var context = new SomeCustomDbContext() // LINQ to Entities? var items = co
JDK 16 now includes a toList() method directly on Stream instances。在以前的Java版本中,您始终必须使用collect方法并提供Co
我真的很困惑如何解决这个问题 public bool DeleteVegetationZone(ref Assessment objAssessment, int VegetationZoneIDT
import numpy as np from scipy.sparse import lil_matrix 使用 numpy 我得到 test_mat = (np.ones((4,6))) test
当我有一个 IEnumerable我不知道它是否是一个列表(根据 List ),我必须枚举 IEnumerable以确保我不会枚举那个可枚举项两次(例如循环两次,或类似的事情)。 Resharper
我不知道 LinqQuery.ToList().Distinct() 和 LinqQuery.Distinct().ToList(); 对我来说有什么区别看起来一样。 考虑这个示例代码: List s
给定以下 LINQ 语句,哪个更有效? 一个: public List GetLatestLogEntries() { var logEntries = from entry in db.Lo
我正在尝试使用 Moq 模拟 Entity Framework DbContext,尤其是它的扩展方法 Add()、Tolist() 和 Find()。 我需要 Find() 和 ToList() 方
这个问题在这里已经有了答案: java 8 Collector is not a functional interface, who can tell why? (2 个回答) Java8: Usin
我只是在尝试新的 kotlin 语言。我遇到了生成无限列表的序列。我生成了一个序列并尝试打印前 10 个元素。但是下面的代码没有打印任何东西: fun main(args: Array) {
我们的代码库中都弹出了这两个代码 pandas.DataFrame.columns.values.tolist() pandas.DataFrame.columns.tolist() 这些总是相同的吗
这些 linq 查询有什么区别: Students.Where(x=>x.City == "Lahore").ToList(); Students.ToList().Where(x=>x.City =
我试图通过创建一个存储前 3 个值的单独变量来过滤数组列表。但是,在集合中出现错误。我对此很陌生,所以任何帮助都会很棒! public static ArrayList exerciseDetail(
我看到以下代码: using(var iterator = source.GetEnumerator()) {...} 哪里source是 IEnumerable . 执行上述操作与转换相比有何优势
我是一名优秀的程序员,十分优秀!