ienumerable

Extension method for Enumerable.Intersperse?

I learned the intersperse function from Haskell, and have been looking for an implementation in c#. Intersperse takes 2 arguments, an IEnumerable<T> source and a T element. It returns an IEnumerable with element inserted between every element of source. One possible use-case is to put an arbitrary interger in between a list of integer...

What's a clean way to break up a DataTable into chunks of a fixed size with Linq?

Update: Here's a similar question Suppose I have a DataTable with a few thousand DataRows in it. I'd like to break up the table into chunks of smaller rows for processing. I thought C#3's improved ability to work with data might help. This is the skeleton I have so far: DataTable Table = GetTonsOfData(); // Chunks should be any ...

break an IEnumerable<int> query which uses Enumerable.Range

Hi I have this following IEnumerable LINQ query: var query = from p in Enumerable.Range(2, 1000000) let sumofPowers = from ch in p.ToString() let sumOfPowers = Math.Pow(Convert.ToDouble(ch.ToString()), 5) select sumOfPowers where p == sumofPowers.Sum() select p; It finds the sum of all the numbers that can ...

Why is .ForEach() on IList<T> and not on IEnumerable<T>?

I've noticed when writing LINQ-y code that .ForEach() is a nice idiom to use. For example, here is a piece of code that takes the following inputs, and produces these outputs: { "One" } => "One" { "One", "Two" } => "One, Two" { "One", "Two", "Three", "Four" } => "One, Two, Three and Four"; And the code: private string InsertCommasAt...

Why can't IEnumerator's be cloned?

In implementing a basic Scheme interpreter in C# I discovered, to my horror, the following problem: IEnumerator doesn't have a clone method! (or more precisely, IEnumerable can't provide me with a "cloneable" enumerator). What I'd like: interface IEnumerator<T> { bool MoveNext(); T Current { get; } void Reset(); // NEW...

Collection was modified; enumeration may not execute error when removing a ListItem from a LIstBox

I have two ListBoxes, lstAvailableColors and lstSelectedColors. Between each listbox are two buttons, Add and Remove. When a color or colors is selected in lstAvailableColors and the Add button is clicked, I want to remove them from lstAvailableColors and display them in lstSelectedColors. Also, if colors are selected in lstSelectedCo...

Does "Select New" in linq trigger an evaluation / load?

I'm currently trying to create a class which implements IEnumerable<T> in order to construct a Hierarchy from a flat list of objects which have references to each other through a ParentId property. I'd like to write a fluent interface for this so I can do something like this IEnumerable<Tab> tabs = GetTabs(); IEnumerable<TabNode> tabNo...

Implementing GetEnumerator in C++

In C# i have the following class and it compiles just fine: class CustomItem { } class CustomList : IList<CustomItem> { public CustomItem this[int index] { get { return null; } set { throw new NotImplementedException(); } } public void CopyTo(CustomItem[] array, int arrayIndex) { } ...

LINQ to resx?

I'm trying to build a way to get the key for a given piece of text in a given resx file at runtime. Currently, I can open and read the file (Using ResXResourceReader) but I have to use a foreach to go over the entire file. This could be a performance issue, as some of our resx files are fairly large (in the order of 2000 strings) and w...

C#: Is it possible to return an IOrderedEnumerable<T>?

Is it possible to return an IOrderedEnumerable<T> from a method without using the OrderBy or OrderByDescending methods on an IEnumerable<T>? Im guessing perhaps not... but... maybe I am wrong? Reason: Mostly curiosity. It just kind of hit me when making this answer on returning digits in a number. And my method would return the digit...

How do I make my Linq to Sql class IEnumerable

How do I make my Linq to Sql class IEnumerable or an object IEnumerable in C# 3.0 ...

How to transform an enumeration of long to a single string with LINQ

Hello all, I am wondering if there is an easy and clean way (one line) to transform an enumeration of long (IEnumerable) to a single string (string) with LINQ? Thanks ...

Is there a way to do a foreach on each element pair in 2 IEnumerable's in C#?

I could convert them to lists and just use a regular for loop with indexes, but I'm wondering if there's a way to do it that keeps them as IEnumerables. ...

Faster enumeration: Leveraging Array Enumeration

So, I have a class with an array inside. Currently, my strategy for enumerating over the class's items is to use the code, foreach (item x in classInstance.InsideArray) . I would much rather use foreach (item x in classInstance) and make the array private. My main concern is that I really need to avoid anything slow; the array gets hi...

Can I implement yield return for IEnumerable functions in VB.NET?

In C#, when writing a function that returns an IEnumerble<>, you can use yield return to return a single item of the enumeration and yield break; to signify no remaining items. What is the VB.NET syntax for doing the same thing? An example from the NerdDinner code: public IEnumerable<RuleViolation> GetRuleViolations() { if (String...

Applying LINQ to Objects Group By and Sort By to generic List<T> (C#)

I am having a hard time creating working Group By and Sort By clauses against a generic List myList. myList has a list of property 'Settings' which itself contains a list of 'child' properties for each business. I want to group by a Industry and within each Industry, sort by business name. My intent would be this: string groupSetting...

Is there a way to check if an IEnumerable is being accessed using a For Each loop?

Suppose I have an IEnumerable such as a List(TValue) and I want to keep track of whether this list is being accessed (to prevent issues with, say, adding to the list while it is being iterated over on a different thread); I can always write code such as the following: Dim List1 As New List(Of Integer) Dim IteratingList1 As Boolean = Fal...

IEnumerable and string array - find matching values

Background: I have an ASP.NET MVC view page with a MultiSelectList in the View Model. I want to populate a label with the list of SelectedValues from that MultiSelectList object. The list is stored within the MultiSelectList with a type of IDName: public class IDName { public int ID {get; set;} public string Name {get; set;} } ...

Checking if no elements in IEnumerable(Of T) - Linq element and quantifier operators

For my function public static IEnumerable<CallbackListRecord> LoadOpenListToProcess(CallbackSearchParams usp){} This line errors when the sequence contains no elements (as it should) CallbackListRecord nextRecord = CallbackSearch.LoadOpenListToProcess(p).First(); I have changed it to the following CallbackListRecor...

Run a method on all objects within a collection

So I have a collection of Razzies created from a Collection of Bloops. I retrieve this collection using a Linq query. Reference:http://stackoverflow.com/questions/923238/linq-select-certain-properties-into-another-object for the query. I would like to know if it is possible to run a method on all of the newly created Razzies before re...