ienumerable

How does Assert.AreEqual determine equality between two generic IEnumerables?

I have a unit test that will test to see if a method that returns the correct IEnumerable. The method builds the IEnumerable using yield return. The class that it is an IEnumerable of is below: enum TokenType { NUMBER, COMMAND, ARITHMETIC, } internal class Token { public TokenType type {get; set;} public string te...

C#: IEnumerator<T> in a using statement

I was curious to see how the SingleOrFallback method was implemented in MoreLinq and discovered something I hadn't seen before: public static T SingleOrFallback<T>(this IEnumerable<T> source, Func<T> fallback) { source.ThrowIfNull("source"); fallback.ThrowIfNull("fallback"); using (IEnumerator<T> iterator...

How do I declare a Field with Anonymous Type (C#)

Hello, In the code below,how do I declare myLine as a public(global) variable? The problem is that I can't use the keyword "var". public static IEnumerable<string> ReadLines(StreamReader reader) { while (!reader.EndOfStream) { yield return reader.ReadLine(); } } private void Filter1(...

LINQ selecting distinct from 4 IEnumerable lists

Imagine four lists, all at least have this Id string property, but may have other properties: public class A //or B, C, D { public string Id { get; set; } //..other properties } //so: List<A> a1 = new List<A>(); List<B> a2 = new List<B>(); List<C> a3 = new List<C>(); List<D> a4 = new List<D>(); I want to select all DISTINCT...

C#: SkipLast implementation

I needed a method to give me all but the last item in a sequence. This is my current implementation: public static IEnumerable<T> SkipLast<T>(this IEnumerable<T> source) { using (IEnumerator<T> iterator = source.GetEnumerator()) { if(iterator.MoveNext()) while(true) { ...

An analog of String.Join(string, string[]) for IEnumerable<T>

class String contains very useful method - String.Join(string, string[]). It creates a string from an array, separating each element of array with a symbol given. But general - it doesn't add a separator after the last element! I uses it for ASP.NET coding for separating with "<br />" or Environment.NewLine. So I want to add an empty r...

Converting an array of type T to an array of type I where T implements I in C#.

I am trying to accomplish something in C# that I do easily in Java. But having some trouble. I have an undefined number of arrays of objects of type T. A implements an interface I. I need an array of I at the end that is the sum of all values from all the arrays. Assume no arrays will contain the same values. This Java code works. Arra...

How can I overcome the overhead of creating a List<T> from an IEnumerable<T>?

I am using some of the LINQ select stuff to create some collections, which return IEnumerable<T>. In my case I need a List<T>, so I am passing the result to List<T>'s constructor to create one. I am wondering about the overhead of doing this. The items in my collections are usually in the millions, so I need to consider this. I assume...

How to prevent memory overflow when using an IEnumerable<T> and Linq-To-Sql?

This question is related to a previous question of mine That's my current code IEnumerable<Shape> Get() { while(//get implementation yield return new Shape(//... } void Insert() { var actual = Get(); using (var db = new DataClassesDataContext()) { db.Shapes.InsertAllOnSubmit(actual); ...

C# Performance of nested yield in a tree

I've got a tree-like structure. Each element in this structure should be able to return a Enumerable of all elements it is root to. Let's call this method IEnumerable<Foo> GetAll(). So if we have A <-- topmost root / \ B C / \ / \ D E F G a call to GetAll on element C returns {C, F, G} (fixed order of eleme...

Explain in small words why IQueryable<T> is needed

There's this. Can you do a better job explaining (in small words so I understand :)) why we need a marker interface which doesn't add any methods over IEnumerable? ...

How do I Aggregate multiple IEnumerables of T

Given.... Public MasterList as IEnumerable(Of MasterItem) Public Class MasterItem(Of T) Public SubItems as IEnumerable(Of T) End Class I would like a single IEnumerable(Of T) which will iterate through all SubItems of all MasterItems in MasterList I would like to think that there is a Linq facility to do this, or an extension me...

Why can i not cast an IEnumerable<T> list to a BindingList<t>?!

Hi There, Is it possible to cast an IEnumerable list to a BindingList collection? The IEnumerable list is a list of typed objects e.g: IEnumerable<AccountInfo> accounts = bll.GetAccounts(u.UserName, u.Password); And my PagingList just extends BindingList: public class PagingList<T> { public BindingList<T> Collection { get; set...

Should I always return IEnumerable<T> instead of IList<T>?

When I'm writing my DAL or other code that returns a set of items, should I always make my return statement: public IEnumerable<FooBar> GetRecentItems() or public IList<FooBar> GetRecentItems() Currently, in my code I have been trying to use IEnumerable as much as possible but I'm not sure if this is best practice? It seemed righ...

What am I Doing Wrong with IQueryable<T>?

I thought IQueryable<T> was derrived from IEnumerable<T>, so why can't I access the results of my query like a collection of records? public bool DoLogIn(System.String strUserName, System.String strPassword) { if (this.IsLoggedIn) return false; ASRDBDataContext ASRData = new ASRDBDataContext(); IQueryable<user> Curr...

Use List<T>ForEach to add element to HashTable

I have a list of string values that I want add to a hashtable or other array that can be accessed by key/index but cannot implement it. I have this working how I want but its ugly List<string> valueList = new List<string>(); valueList.Add("1"); valueList.Add("2"); valueList.Add("3"); Hashtable p ...

Performance regarding cached IEnumerable<T> implementation

[EDIT] The new Reactive Framework solves the problem outlined below, using the System.Linq.EnumerableEx.MemoizeAll() extension method. Internally, MemoizeAll() uses a System.Linq.EnumerableEx.MemoizeAllEnumerable<T> (found in the System.Interactive assembly), which is similar to my ThreadSafeCachedEnumerable<T> (sorta). Here's an awfu...

When items change while being enumerated does it affects the enumeration?

Imagine that during a foreach(var item in enumerable) The enumerable items change. It will affect the current foreach? Example: var enumerable = new List<int>(); enumerable.Add(1); Parallel.ForEach<int>(enumerable, item => { enumerable.Add(item + 1); }); It will loop forever? ...

How do I convert IEnumerable to a custom type in C#?

I am using extension methods OrderBy and ThenBy to sort my custom collection on multiple fields. This sort does not effect the collection but instead returns and IEnumberable. I am unable to cast the IEnumerable result to my custom collection. Is there anyway to change the order of my collection or convert the IEnumerable result to my...

When, if at all, should IEnumerable be used over IEnumerable<T>?

In the example below, if client code using GetPeople wanted to print the name and age of each person to the console, it would have to use reflection (I suppose) to determine that query contained an IEnumerable(Of Person) and then cast it as such to get at its properties. Public Function GetPeople() As IEnumerable Dim query = From p ...