ienumerable

Enumerating Collections that are not inherently IEnumerable ?

When you want to recursively enumerate a hierarchical object, selecting some elements based on some criteria, there are numerous examples of techniques like "flattening" and then filtering using Linq : like those found here : link text But, when you are enumerating something like the Controls collection of a Form, or the Nodes collecti...

Return an empty collection when Linq where returns nothing

I am using the below statement with the intent of getting all of the machine objects from the MachineList collection (type IEnumerable) that have a MachineStatus of i. The MachineList collection will not always contain machines with a status of i. At times when no machines have a MachineStatus of i I'd like to return an empty collection...

What is faster or preferred: IEnumerable<>.ToArray() or .ToList()?

I do next: void Foobar(string[] arr, Dictionary<string, string[]>) { var t = arr.Intersect(dic.Keys).ToList(); // .or ToArray() ? foreach(var item in t) { .. } var j = t.Count; // also I need this } which method is preferred? I could go without any but I need to know the size and I don't want to call IEnuramble....

What determines whether the Powershell pipeline will unroll a collection?

# array C:\> (1,2,3).count 3 C:\> (1,2,3 | measure).count 3 # hashtable C:\> @{1=1; 2=2; 3=3}.count 3 C:\> (@{1=1; 2=2; 3=3} | measure).count 1 # array returned from function C:\> function UnrollMe { $args } C:\> (UnrollMe a,b,c).count 3 C:\> (UnrollMe a,b,c | measure).count 1 C:\> (1,2,3).gettype() -eq (UnrollMe a,b,c).gettype() True ...

Why won't Cast<double>() work on IEnumerable<int>?

Possible Duplicates: Enumerable.Cast<T> extension method fails to cast from int to long, why ? Puzzling Enumerable.Cast InvalidCastException Cast/Convert IEnumerable<T> to IEnumerable<U> ? I'm trying to convert an array of integers to an array of doubles (so I can pass it to a function that takes an array of doubles). The...

Cast Linq IEnumerable to Class that inherits Ienumerable

Hi, I have a resultset class: Public Class AResultSet Implements IEnumerable(Of ConcreteResult) Private _list As List(Of ConcreteResult) Public Sub New() _list = New List(Of ConcreteResult) End Sub Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of ConcreteResult) Implements Syst...

Implement IEnumerable<T> For a List Wrapper

I have a class, which is just a wrapper over a list, i.e., public class Wrapper { public List<int> TList {get;set;} public Wrapper() { TList=new List<int>(); } } I want to make Wrapper inherits from IEnumerable so that I can use the following syntax: Wrapper wrapper = new Wrapper() { ...

Why this ToArray() extension method of IEnumerable throws ArrayTypeMismatchException?

Hello, why the next example throws a System.ArrayTypeMismatchException? New Int16(){4,5,6}.Cast(of UInt16).ToArray() I expected that this line returned a UInt16 array containing 4,5 and 6. Thanks in advance. ...

GetGenericTypeDefinition returning false when looking for IEnumerable<T> in List<T>

Following this question, why does enumerable in this: Type type = typeof(List<string>); bool enumerable = (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable<>)); return false? Edit 1 As the above doesn't work, what would be the best way to determine if a class implements IEnumerable? ...

Is it possible to clone an IEnumerable<T> instance, saving a copy of the iteration state?

I'd like to create a copy of an IEnumerator<T> so that I can restart the enumeration process from a particular location in the collection. Clearly, there is no benefit to doing so for collections that implement IList, since we can remember the index of interest. Is there a clever way to accomplish this task using a combination of yield...

What is the best way to take generically a container in C++ using interfaces (i.e. equivalent of taking IEnumerable as argument in C#)

Hello, I would like a C++ constructor/method to be able to take any container as argument. In C# this would be easy by using IEnumerable, is there an equivalent in C++/STL ? Anthony ...

Add(T object) Is Not a Contract Method Of IEnumerable<T> -- Why?

It struck me as weird that any class that inherits from IEnumerable<T> doesn't need to implement Add(T object), even though if you want to use collection initializers when initializing the class instance, you have to implement Add(T object). Why this is so? ...

Implementing IEnumerable on a tree structure

Based on the work of these guys: dvanderboom.wordpress.com/2008/03/15/treet-implementing-a-non-binary-tree-in-c/ www.matthidinger.com/archive/2009/02/08/asp.net-mvc-recursive-treeview-helper.aspx I am trying to implement a TreeView helper that would be used as such: <%= Html.TreeView("records", Library.Instance.Reco...

Create interface which derives from IEnumerable

Hi, I have a couple of classes all of them deriving from IQueryResult. I need each of those classes to be iterable in foreach loop. Unfortunately foreach loop cannot see GetEnumerator method. I've managed to use it in foreach using dynamic keyword available in .NET 4.0 but then IQueryResult need not to derive from IEnumerable. public ...

Implementing IEnumerable question

Hi, I have 2 classes and in the Persons class I want to add the ability of looping through the name properties of the Person collection using a foreach loop, like this: foreach (string name in Persons.Names) { // do something } How would I do this? These are my classes: class Person { public string Name { ...

Resolving IEnumerable<T> with Unity

Can Unity automatically resolve IEnumerable<T>? Let's say I have a class with this constructor: public CoalescingParserSelector(IEnumerable<IParserBuilder> parserBuilders) and I configure individual IParserBuilder instances in the container: container.RegisterType<IParserSelector, CoalescingParserSelector>(); container.RegisterType<...

Interesting use of the C# yield keyword in Nerd Dinner tutorial

Working through a tutorial (Professional ASP.NET MVC - Nerd Dinner), I came across this snippet of code: public IEnumerable<RuleViolation> GetRuleViolations() { if (String.IsNullOrEmpty(Title)) yield return new RuleViolation("Title required", "Title"); if (String.IsNullOrEmpty(Description)) yield return new RuleV...

C# - Yield gives an unusable type

I have a class and a set of IEnumerables that are using this class to give me a list in a list. (See this question's answer for details.) Here is the code: public IEnumerable<IEnumerable<WorkItemColumn>> EnumerateResultSet(WorkItemCollection queryResults)//DisplayFieldList displayFieldList) { foreach (WorkItem workItem in queryRes...

Use Contains() extension method on arrays in C#

There is a Contains() extension method on IEnumerable; In VB I am able to do this: If New String() {"A", "B"}.Contains("B") Then ' ... End If What would be the equivalent of this in C#? ...

How to convert IEnumerable to Subsonic collection?

Embarrassing question really -- I have Subsonic collection, then I filter out some data using Where. MyColl.Where(it => it.foo()==true) now I would like to pass those data still as Subsonic collection. How to do it (the most properly way)? I checked constructor for Subsonic collection, ToX() methods, and googled. edit: Subsonic 2.x...