ienumerable

Ordering of List<T> and other IEnumerables

Hi all! Can one rely that enumerating on the items of a List is done by the order of their insertion? Does anyone know what the spec says about that? Thanks! ...

Implementing of IEnumerable<T>

I have a code: public sealed class Sequence : IEnumerable<MyClass> { List<MyClass> _elements; public IEnumerator<MyClass> Getenumerator() { foreach (var item in _elements) { yield return item; } } IEnumerator IEnumerable.GetEnumerator()...

Should I return an IEnumerable or IList?

I wish to return an ordered list of items from a method. Should my return type be IEnumerable or IList? ...

Remove items from IEnumerable<T>

i have 2 IEnumerable collections. IENumerable<MyClass> objectsToExcept and IENumerable<MyClass> allObjects. objectsToExcept may contain objects from allObjects. I need to remove from allObjects objects in objectsToExcept. For ex: foreach (var myClass in objectsToExcept) { allObjects.Remove(myClass); } OR allObject.Except(ob...

Pattern for using IEnumerator<T> in interfaces

I have a C# class which needs to process a sequence of items (IEnumerable<T>) across a bunch of methods, so I cannot simply foreach inside a method. I call .GetEnumerator() and pass this IEnumerator<T> around and it works great giving me the flexibility I need while looping through a single sequence. Now I want to allow others to add lo...

Pass a lambda expression in place of IComparer or IEqualityComparer or any single-method interface?

I happened to have seen some code where this guy passed a lambda expression to a ArrayList.Sort(IComparer here) or a IEnumerable.SequenceEqual(IEnumerable list, IEqualityComparer here) where an IComparer or an IEqualityComparer was expected. I can't be sure if I saw it though, or I am just dreaming. And I can't seem to find an extension...

I want to check the Count of an IEnumerable but it's very inneficient. Any help?

Special thanks to Rex M for this bit of wisdom: public IEnumerable<Friend> FindFriends() { //Many thanks to Rex-M for his help with this one. //http://stackoverflow.com/users/67/rex-m return doc.Descendants("user").Select(user => new Friend { ID = user.Elem...

How I can cast the GroupedEnumerable to IEnumerable<T>

I have an expression tree project which works well apart from when I use the GroupBy statement. For statements such as “Where” and “OrderBy” the following line returns a “System.Linq.OrderedEnumerable” var result = theQueryableSource.Provider.Execute(newExp); The following line then works correctly as expected. var test = ((IEnumera...

findall in List<T>

I can't find any FindAll method in my List, how can i select objects from the List that respond to a specific criteria, without using the old iterating method? List<oPage> mylist = new List<oPage>(); my oPage class has a property called Title of type string. I added a few items of oPage inside myList. now i want to select all items ...

Linq IEnumerable Select Question - Can I do all of this inside my select?

Hi All, I had a quick question. Can I do all of this logic inside the select statement? var entries = atisDAO.GetPME(xl, null); response.Data.Detectors = new List<DetectorDetails>(entries.Select(pme => new DetectorDetails {ID = pme.PlaceNum.ToString()})); if(response.Data.Detectors.Any()) { response.Data.Detectors.ForEach(d =>{ ...

Choosing the right Collection/List for my repository

I have a repository: public ObservableCollection<ProjectExpenseBO> GetProjectExpenses() { //Get by query IQueryable<ProjectExpenseBO> projectExpenseQuery = from p in _service.project_expense from e in _service.vw_employee where p.employee_id == e.employee_id select new ProjectExpenseBO() { ...

How is IEnumerable<T> Contra-variant ?

This post (http://blogs.msdn.com/b/brada/archive/2005/01/18/355755.aspx) says IEnumerable<T> is Contra-variant. However type T is co-variant because it is an out parameter. So in what context is IEnumerable<T> Contra-variant ?? Hope I am not confusing! Thanks for the answers in advance! ...

What should I use an IEnumerable or IList?

Can anyone tell me when I should use either. For example, I think I should use an IList when I want to access the .Count of the collection or an individual item, correct? Thank you. ...

How can I return an empty IEnumerable?

Given the following code and the suggestions given in this question, I've decided to modify this original method and ask if there are any values in the IEnumarable return it, if not return an IEnumerable with no values. Here is the method: public IEnumerable<Friend> FindFriends() { //Many thanks to Rex-M for his hel...

Casting objects created in LINQ to SQL to a single master object.

I have an interesting problem to solve that would be helped by successfully casting objects created by LINQ to SQL into a single master object that I could pass around. Here is the scenario at a high level. I have a number of stored procedures that fetch data and then all return the exact same columns. The params into the procs and th...

Exception when serializing class which implements IEnumerable

Hi All, I'm trying to serialize a class using XmlSerializer. This class contains a member of a type which implements IEnumerable. Here is a snippet of the code: [Serializable] public class A{ private A1 m1; privte A2 m2; } [Serializable] public class A1: IEnumerable{ private List _list; } [Serializable] public class A2{ ... } [S...

How do you use a linq to sql join two tables and return the result?

I have been looking on Stack overflow but none of the answers seem to fully sort out this problem. The error I'm getting is: Cannot initialize type 'mvcTest.Models.MakeModelSpec' with a collection initializer because it does not implement 'System.Collections.IEnumerable' I have created a new type like this to get over the anonymous typ...

Serializing result of a LINQ IEnumerable

I have a simple value type: [Serializable] private struct TimerInstance { public TimerInstance(string str, long nTicks) { _name = str; _ticks = nTicks; } private readonly string _name; private readonly long _ticks; public string Name { get { return _na...

What is the best way to find all dependent children in a IEnumerable collection

I have a database with 2 tables: Items ItemDependencies Items has key of ID ItemDependencies have two columns: ItemId, and DependsOnItemId I conver this to a collection: IEnumerable<Item> items = GetItems(); each item has a: Dependencies property which is a List<Item> So i want to filter the initial items list to: Given a ...

Check if one IEnumerable contains all elements of another IEnumerable

What is the fastest way to determine if one IEnumerable contains all the elements of another IEnumerable when comparing a field/property of each element in both collections? public class Item { public string Value; public Item(string value) { Value = value; } } //example usage Item[] List1 = {new Item("1"),n...