ienumerable

How to check an IEnumerable for multiple conditions with a single enumeration without buffering?

让人想犯罪 __ 提交于 2020-07-08 20:31:23
问题 I have a very long sequence of data is the form of IEnumerable , and I would like to check it for a number of conditions. Each condition returns a value of true or false, and I want to know if all conditions are true. My problem is that I can not afford to materialize the IEnumerable by calling ToList , because it is simply too long (> 10,000,000,000 elements). Neither I can afford to enumerate the sequence multiple times, one for each condition, because each time I will get a different

Accessing a range of elements from any point in an IEnumerable

我与影子孤独终老i 提交于 2020-07-08 05:59:38
问题 I have this method: private IEnumerable<XElement> ReadTransactions(string file_name) { using (var reader = XmlReader.Create(file_name + ".xml")) { while (reader.ReadToFollowing("transaction", "urn:namepsaceUri")) { using (var subtree = reader.ReadSubtree()) { yield return XElement.Load(subtree); } } } } This method reads from an XML file. However, I don't need all of the nodes in the XML file at same time. I want to get them ten at a time. I tried working with XPathSelectElements, but that

Is there a way to call a C# method requiring an IEnumerable<T> with a single value? …with benchmarking [duplicate]

亡梦爱人 提交于 2020-06-26 12:24:27
问题 This question already has answers here : Passing a single item as IEnumerable<T> (18 answers) Closed 15 days ago . I'm just wondering if I'm missing out on some syntactic sugar or micro-optimization... If I'm calling a method requiring an IEnumerable of values but I only have a single value, then I put that single value in an array. for example: var x = 1.23; SquareAllTheNumbers(new[] { x }); private void SquareAllTheNumbers(IEnumerable<double> values) { foreach (var value in values) {

Maintain subsequence order in OrderBy

我只是一个虾纸丫 提交于 2020-06-22 11:55:10
问题 Suppose I have a class with an integer Day property and an IEnumerable<T> of objects where the days are 2, 3, 4, 1, 3, 3 and 5 (in that order). Is there a way to guarantee that the order of the subsequence where (for example) o.Day == 3 is maintained from its elements' relative positions in the original list without requiring an explicit, custom implementation of IEnumerable ? 回答1: OrderBy is documented as being stable, if that is what you mean; so: you shouldn't need to do anything.

Easiest method to OrderBy a String using StringComparison.Ordinal

梦想的初衷 提交于 2020-06-14 08:35:25
问题 I've found a bug (in my code) that results from String.CompareTo and binary search because my custom IComparer (for the wrapping type) uses String.Compare(x, y, StringComparison.Ordinal) . This is because items.OrderBy(i => i.Name) (where Name is of type string) used to build the Array to search used the string object itself as the IComparable - and such has different rules: The comparison uses the current culture to obtain culture-specific information such as casing rules and the alphabetic

Easiest method to OrderBy a String using StringComparison.Ordinal

孤人 提交于 2020-06-14 08:33:10
问题 I've found a bug (in my code) that results from String.CompareTo and binary search because my custom IComparer (for the wrapping type) uses String.Compare(x, y, StringComparison.Ordinal) . This is because items.OrderBy(i => i.Name) (where Name is of type string) used to build the Array to search used the string object itself as the IComparable - and such has different rules: The comparison uses the current culture to obtain culture-specific information such as casing rules and the alphabetic

How to cast property of IEnumerable<DirectoryInfo> to List<string>

随声附和 提交于 2020-04-17 22:45:59
问题 I'm running the following code to enumerate all the available folders and subfolders on a network share, DirectoryInfo dirInfo = new DirectoryInfo(path); var dirList = dirInfo.EnumerateDirectories("*.", SearchOption.AllDirectories); foreach (var d in dirList) { Console.WriteLine(d.FullName); } The code itself works absolutely fine. However, rather than declare the var dirList , I'd like to use a List<string> variable directly, but it's not clear to me how to do this. 回答1: This will extract

How to cast property of IEnumerable<DirectoryInfo> to List<string>

醉酒当歌 提交于 2020-04-17 22:41:23
问题 I'm running the following code to enumerate all the available folders and subfolders on a network share, DirectoryInfo dirInfo = new DirectoryInfo(path); var dirList = dirInfo.EnumerateDirectories("*.", SearchOption.AllDirectories); foreach (var d in dirList) { Console.WriteLine(d.FullName); } The code itself works absolutely fine. However, rather than declare the var dirList , I'd like to use a List<string> variable directly, but it's not clear to me how to do this. 回答1: This will extract

LINQ入门笔记----LINQ To Object<SelectMany()>

拈花ヽ惹草 提交于 2020-03-30 19:29:46
LINQ To Object 概述 LINQ To Object是用于操作内存对象的LINQ编程接口,包含了大量的查询操作符,针对内存中的集合对象进行操作. LINQ To Object的实现基于IEnumerable泛型接口、序列(sequences)以及标准查询操作符(Standard Query Operators)等基本概念.其中,IEnumerable泛型接口是使用C#2.0泛型技术实现的一个接口,该接口与IEnumerable类似,允许对接口内部的元素进行列举操作;序列是一个专门术语,表示一个实现了IEnumerable接口的集合对象. LINQ To Object的大部分操作是针对序列的.标准查询操作符本质上是一些拓展方法,这些拓展方法定义在静态类 System.Linq.Enumerable 中,其原型的第一个参数(带this修饰符的参数)是IEnumerable类型.由于这些方法都是扩展方法,它们可以在IEnumerable实例对象上直接调用,无需为调用传递一个类型为IEnumerable的对象作为第一个参数. 要在代码中使用LINQ To Object标准查询操作符,需要在代码中添加 using System.Linq 指令,以引入必要的命名空间. SelectMany() <SelectMany 的参数信息> SelectMany 将序列的每个元素投影到

C#迭代器

懵懂的女人 提交于 2020-03-30 05:24:08
摘要:迭代器是C#2.0中添加的功能,它能够使我们在类或结构中支持foreach迭代,而不必实现整个IEnumerable/IEnumerable接口。今天我们就一块看一下什么是c#中的迭代器吧。 主要内容: 1.foreach的运行机制 2.传统集合的遍历 3.使用迭代器 一、foreach的运行机制 我们在程序中经常会用到foreach,如果你把它理解成是for的一种简写形式的话那就太大材小用了,事实上foreach中包含了丰富的内容。我们知道要使用foreach遍历集合就必须实现IEnumerable接口,而要实现IEnumerable接口就要实现IEnumerator接口。关于如何实现这两个接口我们在第二部分会看到,在谈foreach的运行机制之前请允许我使用msdn中的Person类(我们下面的几部分中我们还会用到相关的People和PeopleEnum类): View Code 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace IteratorDemo 7 { 8 class Person 9 { 10 public Person( string fName, string lName) 11 { 12