ienumerable

Enumeration of .Net IList

I'd like to know if I can assume that the IEnumerator I get from an IList (by calling the GetEnumerator method from the IEnumerable interface) will give me the items in the order of the list. What do you think? ...

VB.NET equivalent to this C# linq class

Guys, I'm trying to convert something from C# to VB.NET and I'm having trouble finding an equivlent in VB.NET to C#'s yield keyword. I realize 'yield' is not a convertable keyword to VB.NET, so can someone please show me how I would implement this code in VB.NET. I got all of it converted over except for the implemented GetEnumerator() f...

What is the easiest and most compact way to create a IEnumerable<T> or ICollection<T>?

So, many times we have a function that accepts an IEnumerable or ICollection as a parameter. In cases where we have single items, but no collection to hold them, we must create a collection before passing them to the function, like: T o1, o2, o3; Foo(new T[] { o1, o2, o3 }); I've always created an array or a list, like I've done in th...

C# Silverlight with Entity Framework - Change Return Type On AutoGenerated EntityQuery!?

Background Currently I have a C# Silverlight business application which uses RIA Services. The application is hosted in ASP.NET using the ADO.NET Entity Framework and a domain service class to read and write to the SQL Server database. Scenario I have a server-side method in my DomainServiceClass that returns an IEnumerable list of ob...

C#: How do you test the IEnumerable.GetEnumerator() method?

Let's say I for example have this class that generates Fibonacci numbers: public class FibonacciSequence : IEnumerable<ulong> { public IEnumerator<ulong> GetEnumerator() { var a = 0UL; var b = 1UL; var c = a + b; while (true) { yield return c; c = a + b; ...

C#: Where to implement a custom IEnumerator<T>

Say I have a class that implements IEnumerable<T>. It currently uses the yield keyword in the GetEnumerator() method. But now I need to do a bit more, for example I would like to clean up after myself. To do this, unless I have overlooked anything, I need to implement the IEnumerator<T> interface. But where would you say I should do that...

"No data exists for the row/column" exception after using ToList

I have an extension method to convert a DbDataReader to an IEnumerable object: public static IEnumerable<IDataRecord> AsEnumerable(this DbDataReader reader) { while (reader.Read()) { yield return reader; } } In my application, I query the database like so: var records = context.Query("select WRKORDNBR from WRKORDER")....

Enumerable.Range implementation

What is the precise implementation of Enumerable.Range in .Net; preferable .Net 4? Is it a yielded for-loop? A custom implementation (IEnumerable, IEnumerator) or? ...

enumerate through all IEnumerables

Hi. I need to send different IEnumerables to an Printer object. This printer object will then do something to them, inside a foreach loop. class Printer { public Printer(IEnumerable list) { foreach (var enumerable in list) { //DO STUFF } } } This lets me send any enumerable, such as an ...

Caching IEnumerable

public IEnumerable<ModuleData> ListModules() { foreach (XElement m in Source.Descendants("Module")) { yield return new ModuleData(m.Element("ModuleID").Value); } } Initially the above code is great since there is no need to evaluate the entire collection if it is not needed. However, once all the Modules have been ...

Several unusual errors when attempting to convert a string[] to a Dictionary<short, string>

I have the following code that splits a string on newlines and converts it to a Dictionary for further processing: string[] splitProgram = program.Split(Environment.NewLine.ToCharArray()); short i = 0; Dictionary<short, string> programDictionary = splitProgram.ToDictionary<short, string>((value) => i++); What's...

How does Linq use IEnumerable methods after an IOrderedEnumerable method?

In Linq, extension methods like Where return an IEnumerable collection, but sorting methods like OrderBy return an IOrderedEnumerable collection. So, if you have a query that ends with OrderBy (i.e. returns an IOrderedEnumerable), you can't later append a Where method - the compiler complains about the type being passed into Where. var...

Convert CollectionBase to List or data type usable with Linq

Hi I am using Aspose cells to manipulate Excel spreadsheets. One of the types in the API is a collection of Pictures in the spreadsheet, which derives from CollectionBase: see this link: http://www.aspose.com/documentation/.net-components/aspose.cells-for-.net/aspose.cells.pictures.html I want to convert this type to something that al...

Is it possible to get an IEnumerator<T> from a T[]?

Let's say I want to create a collection class that is thread-safe by default. Internally, the class has a protected List<T> property called Values. For starters, it makes sense to have the class implement ICollection<T>. Some of this interface's members are quite easy to implement; for example, Count returns this.Values.Count. But imp...

How to profile an application when deferred execution makes it difficult?

I have this .NET application, which relies on deferred execution. When I am profiling it the methods that consume most time are those who enumerates the IEnumerables. Because of this I think that the methods that must be optimized are not in the Top Time Consuming methods. Did it ever happened to you? How to profile an application corr...

Passing a single item as IEnumerable<T>

Is there a common way to pass a single item of type T to a method which expects an IEnumerable<T> parameter? Language is C#, framework version 2.0. Currently I am using a helper method (it's .Net 2.0, so I have a whole bunch of casting/projecting helper methods similar to LINQ), but this just seems silly: public static class IEnumerab...

Enumerators and Thread-safety

Just to make sure, say that i have this code: this.allObjects = [some linq query]; and have two methods that both read (not modify) this IEnumerable, are they safe to call in parallel? Just looping through a IEnumerable should be safe right? ...

How to create a enumerator/generator of strings or integers?

I want to have a enumerator/generator that will always provide me with the next value whenever I call say GetNext? Is this possible? Examples: myStringEnumerator.GetNext() -> returns "Identifier01.xml" myStringEnumerator.GetNext() -> returns "Identifier02.xml" myStringEnumerator.GetNext() -> returns "Identifier03.xml" myStringEnu...

How can I reduce IEnumerable<IEnumerable<Foo>> to IEnumerable<Foo>?

Sorry for the weird caption. What I'm trying to achieve is simple: IEnumerable<IEnumerable<Foo>> listoflist; IEnumerable<Foo> combined = listoflist.CombineStuff(); Example: {{0, 1}, {2, 3}} => {0, 1, 2, 3} I'm positive there is a Linq expression for this... Sidenote: The lists may be large. ...

IEnumerator<T> Implementation

Hi, I have a this code public class SomeClass<T>: IEnumerable<T> { public List<SomeClass<T>> MyList = new List<SomeClass<T>>(); public IEnumerator<T> GetEnumerator() { throw new NotImplementedException(); } } How can I Extract a IEnumerator from MyList ? Thanks StackoverFlower.... ...