LINQ To Xml:迭代中的万圣节问题

38 篇文章 0 订阅

最近在研究操作xml的最好方式,随便看了几个英文版电子书,上面都提到DOM 和 linq to xml方式的比较

linq to xml的方法,简洁直观,代码量小,下面扯扯本文想要说的万圣节问题,这个词从以下书中来

Pro LINQ: Language Integrated Query in C# 2010

 

第231页,(Halloween Problem)

 

那么什么是万圣节问题呢,不说名字怎么来了,书里解释了一下。下面说一下,具体指的是什么

万圣节问题指在迭代一组元素的时候,删除或改变元素个数时,迭代出现的的不正常行为,但这种问题可以避免,同时达到本来要达到的效果

 

书中的例子

 

 

 

理论上在foreach的时候,每次都调用remove了,所以每次都应该打印出删除的元素,并且父元素下的子

元素应该全部都删除掉了,可是打印出的结果并非如此

 

 

 

竟然还剩一个元素没有删除掉!并且也只打印出第一个元素删除掉的那句话。

费解吗?其实linq的执行都是延迟执行的,这是其导致的潜在副作用

书中给出的解决方案是,缓存元素序列,其中用到了 ToArray 操作符

 

 

 

注意LINQ To XML 构建XML树的时候是延迟查询,也就是Console.WriteLine(xDocument); 这句才真正生成XML树,故之前的循环如果不缓存起来,是不可能操作到XML树的


注意这句 XElement element in elements.ToArray()

ToArray在MSDN文档中

http://msdn.microsoft.com/zh-cn/library/bb298736.aspx

有这么一句话

ToArray<TSource>(IEnumerable<TSource>) 方法强制进行直接查询计算,并返回一个包含查询结果的数组。 可将此方法追加到您的查询,以获得查询结果的缓存副本。

 


接着打印结果变成我们最终想要的那样

 

 

 

如此这般,问题就解决了

我终于也明白为什么之前我在forearch datarow in datatable.rows时候我使用row.delete时总是出现问题了 :)

 

有些人说我早就知道迭代中不能这样,但你知道原理吗?你知道怎么防止迭代中的错误吗?我估计你也只是看开头的那个黑体字就没往下看了吧,呵呵,你知道不代表你高明,更不用特意表现出来,此文章仅供学习之用,如果你知道,那么请绕道,这是给不知道的人看的,谢谢!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
LINQ is the part of the .NET Framework that provides a generic approach to querying data from different data sources. It has quickly become the next must-have skill for .NET developers. Pro LINQ: Language Integrated Query in C# 2010 is all about code. Literally, this book starts with code and ends with code. Most books show the simplest examples of how to use a method, but they so rarely show how to use the more complex prototypes. This book is different. Demonstrating the overwhelming majority of LINQ operators and prototypes, it is a veritable treasury of LINQ examples. Rather than obscure the relevant LINQ principles in code examples by focusing on a demonstration application you have no interest in writing, this book cuts right to the chase of each LINQ operator, method, or class. However, where complexity is necessary to truly demonstrate an issue, the examples are right there in the thick of it. For example, code samples demonstrating how to handle concurrency conflicts actually create concurrency conflicts so you can step through the code and see them unfold. Face it, most technical books, while informative, are dull. LINQ need not be dull. Written with a sense of humor, this book will attempt to entertain you on your journey through the wonderland of LINQ and C# 2010. What you’ll learn How to query Databases with LINQ to SQL, write your own entity classes, and understand how to handle concurrency conflicts. * How to leverage all the new LINQ relevant C# 2008 language features including extension methods, lambda expressions, anonymous data types, and partial methods. * How to use LINQ to Objects to query in-memory data collections such as arrays, ArrayLists, and Lists to retrieve the data you want. * Why some queries are deferred, how a deferred query can bite you, and how you can make deferred queries work for you. * How to use LINQ to XML to revolutionize your creation, manipulation, and searching of XML data. * How to query DataSets with LINQ to DataSet so you can coexist with legacy code and use LINQ to query databases other than SQL Server. Who is this book for? Apress, 2010 This book is written for the proficient C# developer, but you do not need to be up on all the latest C# features to understand the material. When you finish this book, you will have a much greater understanding of the latest C# features. amazon link:http://www.amazon.com/exec/obidos/ASIN/1430226536/buythisbooks-20
关于LINQ的一本书籍,附上源码,喜欢请购买正版。 LINQ is the part of the .NET Framework that provides a generic approach to querying data from different data sources. It has quickly become the next must-have skill for .NET developers. Pro LINQ: Language Integrated Query in C# 2010 is all about code. Literally, this book starts with code and ends with code. Most books show the simplest examples of how to use a method, but they so rarely show how to use the more complex prototypes. This book is different. Demonstrating the overwhelming majority of LINQ operators and prototypes, it is a veritable treasury of LINQ examples. Rather than obscure the relevant LINQ principles in code examples by focusing on a demonstration application you have no interest in writing, this book cuts right to the chase of each LINQ operator, method, or class. However, where complexity is necessary to truly demonstrate an issue, the examples are right there in the thick of it. For example, code samples demonstrating how to handle concurrency conflicts actually create concurrency conflicts so you can step through the code and see them unfold. Face it, most technical books, while informative, are dull. LINQ need not be dull. Written with a sense of humor, this book will attempt to entertain you on your journey through the wonderland of LINQ and C# 2010. What you’ll learn How to leverage all the new LINQ relevant C# 2008 language features including extension methods, lambda expressions, anonymous data types, and partial methods. How to use LINQ to Objects to query in-memory data collections such as arrays, ArrayLists, and Lists to retrieve the data you want. Why some queries are deferred, how a deferred query can bite you, and how you can make deferred queries work for you. How to use LINQ to XML to revolutionize your creation, manipulation, and searching of XML data. How to query DataSets with LINQ to DataSet so you can coexist with legacy code and use LINQ to query databases other than SQL Server. How to query Databases with LINQ to SQL, write your own entity classes, and understand how to handle concurrency conflicts. Who this book is for This book is written for the proficient C# developer, but you do not need to be up on all the latest C# features to understand the material. When you finish this book, you will have a much greater understanding of the latest C# features. Table of Contents Hello LINQ C# Language Enhancements for LINQ LINQ to Objects Introduction Deferred Operators Nondeferred Operators LINQ to XML Introduction The LINQ to XML API LINQ to XML Operators Additional XML Capabilities LINQ to DataSet Operators Additional DataSet Capabilities LINQ to SQL Introduct ion LINQ to SQL Tips and Tools LINQ to SQL Database Operations LINQ to SQL Ent ity Classes The LINQ to SQL DataContext LINQ to SQL Concurrency Conflicts Additional LINQ to SQL Capabilities LINQ to Entities Introduction LINQ to Entities Operations LINQ to Entities Classes Parallel LINQ Introduction Using Parallel LINQ Parallel LINQ Operators
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值