ienumerable

Is it possible to have a property that is IEnumerable<T>?

I have a class that's IEnumerable<T> where I want to have different properties that provides a filtered IEnumerable<T> access. So for instance: class Shape ShapeType = Box/Sphere/Pyramid class ShapeCollection : IEnumerable<Shape> { public IEnumerable<Shape> OnlyBox { foreach(var s in this) { if (s.Sha...

Is it possible to write a recursive IEnumerable<T>

I have a class like: class Spline int ChildrenCount; Spline GetChild (int index) class SplineCollection : IEnumerable<Spline> Spline Master Is it possible to write a recursive IEnumerable for the SplineCollection where it will return all the children one by one? EDIT: So Master is the root Box, and the hierarchy of its c...

What's your favorite LINQ to Objects operator which is not built-in?

With extension methods, we can write handy LINQ operators which solve generic problems. I want to hear which methods or overloads you are missing in the System.Linq namespace and how you implemented them. Clean and elegant implementations, maybe using existing methods, are preferred. ...

WebMethod and IEnumerable

I need to create WebMethod that will get some data from db and return that to the client. Now, assume that the amount of data is huge, so I'd like to take and return data in parts. is there any way to use yield return in Webmethod? as I know there is no way to return generic types in WebMethods but I couldn't use non-generic IEnumer...

Converting a generic collection of a concrete type to a collection of a base type

I've got a number of classes that implement a specific interface (ISearchable) and I'd like to return an IEnumerable of the base type (ISearchable) from a static method, but I'm not sure how to convert it without making intermediate collections. The code is pretty simple, one of the domain objects' implementations is like so: public cl...

How do you deal with sequences of IDisposable using LINQ?

What's the best approach to call Dispose() on the elements of a sequence? Suppose there's something like: IEnumerable<string> locations = ... var streams = locations.Select ( a => new FileStream ( a , FileMode.Open ) ); var notEmptyStreams = streams.Where ( a => a.Length > 0 ); //from this point on only `notEmptyStreams` will be used/v...

Custom ModelBinder for IEnumerable post result in MVC

Consider this view model that uses two custom validators. It posts back to the controller in an enumerable collection. public class FormFieldViewModel { public Guid FormFieldKey { get; set; } public string Name { get; set; } // Utilize a string[] array since multiple-selection items will require this (we can assume array[0]...

How to select an element in a List of N using Where extension function?

Hello, Suppose I have a class AddressType defined as is: public class AddressType { public int AddressTypeId { get; set; } public string Description { get; set; } } Having a List object in code, how do I select an AddressType object with a known AddressTypeId property? I have never used the List.Where extension function.......

How FirstOrDefault extension method works?

I was wondering on how FirstOrDefault extension method works? Which one of the following algorithms does it follows? Use: var arr = new[] {1, 2, 3, 4, 5, 6, 7}; return arr.FirstOrDefault(x => x%2 == 0); Algorithm 1: for(int i = 0; i < arr.Length; i++) { if(arr[i] % 2 == 0) return arr[i]; } return 0; Algorithm 2: var list ...

mvc.net how to populate dropdownlist with enum values

I have an enum for one of the properties of my view-model. I want to display a drop-down list that contains all the values of the enum. I can get this to work with the following code. What I'm wondering is whether there is a simple way to convert from an enum to an IEnumerable? I can do it manually as in the following example, but wh...

Compare type of IEnumerable<T>

I have dictionary that gives me back a method according to the value passed in. Defined as so: Dictionary<Type, IXmlWriterConverter> I have now added a new function that which has the Key/type set to IEnumerable, so far so good. But when I execute my unit test with a List containing two DataTables but the dictionary can not find the k...

How can I detect "missing" elements in an IEnumerable<T>?

I've got an IEnumerable<T> containing a list of data elements with consistent intervals in one of the properties: List<Interval> list = new List<Interval> { new Interval{ TIME_KEY = 600}, new Interval{ TIME_KEY = 605}, new Interval{ TIME_KEY = 615}, new Interva...

What is the exact difference between returning an IEnumerable instance and the yield return statement in C#

Currently I'm working with some libraries applying deferred execution via iterators. In some situations I have the need to "forward" the recieved iterator simply. I.e. I have to get the IEnumerable<T> instance from the called method and return it immediately. Now my question: Is there a relevant difference between simply returning the r...

A question about IEnumerator.GetEnumerator in C#

Hi there, I have a question about the IEnumerator.GetEnumerator() method. public class NodeFull { public enum Base : byte {A = 0, C, G, U }; private int taxID; private List<int> children; public int TaxID { get { return taxID; } set { taxID = value; } } public int this[int i] { g...

Why implement IEnumerable(T) if I can just define ONE GetEnumerator?

Update: I appreciate all of the comments, which have essentially comprised unanimous opposition. While every objection raised was valid, I feel that the ultimate nail in the coffin was Ani's astute observation that, ultimately, even the one miniscule benefit that this idea ostensibly offered -- the elimination of boilerplate code -- was ...

What is the difference between ((IEnumerable)source).OfType<T>() and source as IEnumerable<T>

What is the difference between ((IEnumerable)source).OfType<T>() and source as IEnumerable<T> For me they look similar, but they are not! source is of type IEnumerable<T>, but it is boxed as an object. Edit Here is some Code: public class PagedList<T> : List<T>, IPagedList { public PagedList(object source, int index, int pageSiz...

How to select the first three elements of a IEnumerable object?

That's it. The question is in the title I was looking for a cleaner way than the using for...break; thanks ...

Error wrong type?

The model item passed into the dictionary is of type '...', but this dictionary requires a model item of type '...' Does anyone know how to solve this error? My controller class: public class MapsController : Controller { public ActionResult Index() { return View(Project.Find(68)); } //[AutoRefresh(Duratio...

Convert collection of IEnumerable using LINQ

I have a IEnumerable collection of Car objects A Car has a property: Year Using LINQ, I want to find where there are > 1 cars with the same year and return that list. I would expect it to have to return an array of lists because if the collection is: Car 1: Year 2010 Car 2: Year 2010 Car 3: Year 2009 Car 4: Year 2009 Car 5: Year 2010...

Using LINQ, how do I choose items at particular indexes?

If I have an IEnumerable<Foo> allFoos and an IEnumerable<Int32> bestFooIndexes, how can I get a new IEnumerable<Foo> bestFoos containing the Foo entries from allFoos at the indexes specified by bestFooIndexes? ...