ienumerable

Yield keyword value added?

still trying to find where i would use the "yield" keyword in a real situation. I see this thread on the subject http://stackoverflow.com/questions/39476/what-is-the-yield-keyword-used-for-in-c but in the accepted answer, they have this as an example where someone is iterating around Integers() public IEnumerable<int> Integers() { y...

Why is my IEnumerable<String> using yield return slower to iterate then List<String>.

I have been testing out the yield return statement with some of the code I have been writing. I have two methods: public static IEnumerable<String> MyYieldCollection { get { wrapper.RunCommand("Fetch First From Water_Mains"); for (int row = 0; row < tabinfo.GetNumberOfRows() ; row++) //GetNumber...

Why foreach statements accept objects that implement the 'Collection' pattern instead of accept only objects that implement IEnumerable?

Many people ask me why, and I don`t have a good answer for them. Obviously there is a good reason. Does anyone know it? I searched here and found this question. It explains how it works, but not why. ...

How to remove objects from an Enumerable collection in a loop

Duplicate Modifying A Collection While Iterating Through It Has anyone a nice pattern to allow me to get around the inability to remove objects while I loop through an enumerable collection (eg, an IList or KeyValuePairs in a dictionary) For example, the following fails, as it modifies the List being enumerated over during the forea...

Why is Enumerable.Range faster than a direct yield loop?

The code below is checking performance of three different ways to do same solution. public static void Main(string[] args) { // for loop { Stopwatch sw = Stopwatch.StartNew(); int accumulator = 0; for (int i = 1; i <= 100000000; ++i) { accumulator +...

C# collection

int[] mylist = { 2, 4, 5 }; IEnumerable<int> list1 = mylist; list1.ToList().Add(1); // why 1 does not get addedto list1?? ...

Design pattern for aggregating lazy lists

I'm writing a program as follows: Find all files with the correct extension in a given directory Foreach, find all occurrences of a given string in those files Print each line I'd like to write this in a functional way, as a series of generator functions (things that call yield return and only return one item at a time lazily-loaded)...

What is the real advantage of returning ICollection<T> instead of a List<T>?

I've read a couple of blog posts mentioning that for public APIs we should always return ICollection (or IEnumerable) instead of List. What is the real advantage of returning ICollection instead of a List? Thanks! ...

Binding ASP.Net Menu Control to a Collection

hi everyone, i am trying to use this code to bind my asp.net menu control to a collection.. but its giving me an error that my collection is now IHierarchyEnumerable.. which I understand why too.. StringCollection sc = pos.getAllmembers(); Menu1.DataSource = pos.getAllmembers().GetEnumerator(); is there a way around this.....

C# Distinct on IEnumerable<T> with custom IEqualityComparer

Hi! Here's what I'm trying to do. I'm querying an XML file using LINQ to XML, which gives me and IEnumerable<T> object, where T is my "Village" class, filled with the results of this query. Some results are duplicated, so I would like to perform a Distinct() on the IEnumerable object, like so: public IEnumerable<Village> GetAllAlliance...

IEnumerable interface

I don't understand why IList implements IEnumerable taking in consideration that IList implements ICollection that implements IEnumerable also. ...

FindLast on IEnumarable

I would like to call FindLast on a collection which implements IEnumarable, but FindLast is only available for List. What is the best solution? ...

When should I use IEnumerator for looping in c#?

I was wondering if there are any times where it's advantageous to use an IEnumerator over a foreach loop for iterating through a collection? For example, is there any time where it would be better to use either of the following code samples over the other? IEnumerator<MyClass> classesEnum = myClasses.GetEnumerator(); while(classesEnum.M...

Linq To Sql return from function as IQueryable<T>

Ok, I have managed to get the following working public IQueryable getTicketInformation(int ticketID) { var ticketDetails = from tickets in _context.tickets join file in _context.file_objects on tickets.ticket_id equals file.source_id where tickets.ticket_id == ticketID select new { tickets.ticket_id, tickets....

C# Freely convert between List<T> and IEnumerable<T>

How can I convert a List to an IEnumerable and back again. The reason I want to do this is to run a series of LINQ statements on the List i.e. sort etc ...

Dynamic LINQ on IEnumerable?

Hi, Say i need to filter a generic list with a dynamic query (List<string> l; var x = l.Where(*dynamic query*)) How would i do this using LINQ? (Currently using a row filter on a dataview) I've seen a posting by scott g: but it appears to not work with objects that use IEnumerable (generic lists included) Can anyone offer any ideas?...

ReadOnlyCollection or IEnumerable for exposing member collections?

Is there any reason to expose an internal collection as a ReadOnlyCollection rather than an IEnumerable if the calling code only iterates over the collection? class Bar { private ICollection<Foo> foos; // Which one is to be preferred? public IEnumerable<Foo> Foos { ... } public ReadOnlyCollection<Foo> Foos { ... } } /...

Best way to convert an IEnumerable<T> to an T[]

What is the best way to convert from a generic IEnumerable object to an array of the same type? The current solution I have looks like the following: IEnumerable<string> foo = getFoo(); string[] bar = new List<string>(foo).ToArray(); The transfer through a List<T> seems unneccesary, but I haven't been able to find a better way to do ...

How do you transfer the execution of a Expression created by an IQueryable object to a IEnumerable ?

In my code I'd like to make my repositories IQueryable. This way, the criteria for selection will be a linq expression tree. Now if I want to mock my repository in theorie this is very easy : just implement the interface of my repository (which is also a IQueryable object). My mock repository implementation would be only a in memory co...

Convert Dataset to IQueryable<T> or IEnumerable<T>

Since there is no Linq to DB2 yet (c'mon IBM!), and I want to deal with IQueryables or IEnumerables in my code, how would I convert a DataTable to an IQueryable? Or an IEnumerable? I have an interface and a class that matches the columns in the datatable... IQueryable<IMyData> GetAS400Data(..parameters..) { DataSet d = GetData(); ...