ienumerable

IEnumerable is empty?

I know it probably doesnt matter/affect performance for the most part but I hate the idea of getting an IEnumerable and doing .Count(). Is there a IsEmpty or NotEmpty or some function? (similar to stl empty()) ...

linq extension method to take elements from the end of the sequence

Hi There is the enumerable extension method Take<TSource>( IEnumerable<TSource> source, int count ) which takes the first count elements from the start. Is there a way to take the elements from the end? or even better a way to take the elements from an offset to the end? Thanks ...

ASP.NET MVC - Insert or Update view with IEnumerable model

I've seen plenty of examples (NerdDinner, Sanderson's Sports Store, etc.) where a view is bound to a collection of objects. The syntax in the view is usually something like this... <%@ Page... Inherits="System.Web.Mvc.ViewPage<IEnumerable<MyViewModel>>" %> ... <% foreach (var myViewModel in Model) { %> I've also seen plenty of exampl...

How to check if type is IEnumerable<BaseType>?

Should be easy... class Base{} class Foo:Base{} public bool Bar(Type t){ // return ??? // NB: shouldn't know anything about Foo, just Base } Assert.True(Bar(typeof(IEnumerable<Foo>)); Assert.False(Bar(typeof(IEnumerable<Base>)); Assert.False(Bar(typeof(string)); Assert.False(Bar(typeof(Foo)); Just to answer question why 2n...

IEnumerable OrderBy

I'm using IEnumerable orderby to sort my items in ascending format but it does not work my query is like this: IEnumerable<Step> steps = allsteps.Where(step => step.X <= Y); steps = steps.OrderBy(step => step.X); its not deffer to use OrderBy or OrderByDescending why? I'm want to use Sum() method to sum some items and item orders i...

Translation of yield into VB.NET

Hello, first i must assume that i'm not very familiar with the C# yield keyword and its function. What is the best/easiest way to "translate" it into VB.NET? Especially i tried to convert this code into VB.NET, but i failed with: yield return new MatchNode(++index, current.Value); What i have is: Imports System.Collections Imports ...

C# batching enumerator

Possible Duplicate: LINQ Partition List into Lists of 8 members. I've got an IEnumerable<T> and I'd like to transform it into an IEnumerable<List<T>> where each List is a batch of items in the same order as the original enumerator. Each batch should be batchSize items in length, except for the last batch which should contain l...

Wrap an IEnumerable and catch exceptions

I've got a bunch of classes that can Process() objects, and return their own objects: public override IEnumerable<T> Process(IEnumerable<T> incoming) { ... } I want to write a processor class that can wrap one of these processors, and log any uncaught exceptions that the wrapped Process() method might throw. My first idea was somethi...

What does the source code for IEnumerable look like?

Hi everyone, I'm in the process of learning Interfaces. I've been reading through some books/articles and so far so good - I've written a few sample Interfaces of my own. Yay :) Now, I've noticed that one of the most popular C# Interfaces around is the IEnumerable Interface. It's used quite a lot for all sorts of collections etc.. Any...

Is this an F# bug?

I have an type that is implementing IEnumerable<T> interface, all is ok: open System type Bar() = interface Collections.IEnumerable with member x.GetEnumerator () = null interface Collections.Generic.IEnumerable<int> with member x.GetEnumerator () = null But things goes wrong if type inherits IEnumerable...

Is this an F# compiler bug? #2

open System type Foo() = interface Collections.IEnumerable with member x.GetEnumerator () = null type Bar() = interface Collections.IEnumerable with member x.GetEnumerator () = null interface Collections.Generic.IEnumerable<int> with member x.GetEnumerator () = null let xs, ys = Foo(), Bar() ...

Is there ever a reason to not use 'yield return' when returning an IEnumerable?

Simple example - you have a method or a property that returns an IEnumerable and the caller is iterating over that in a foreach() loop. Should you always be using 'yield return' in your IEnumerable method? Is there ever a reason not to? While I understand that it may not always be necessary to, or even "better" (maybe it's a very smal...

Dual enumerators, second one via interface- type collisions on implementation

I have a generic class that implements IList public class ListBase<T>: IList<T>, IListBase{ IEnumerator<T> GetEnumerator(){ .... System.Collections.IEnumerator GetEnumerator(){ return GetEnumerator();} } The IListBase is an interface I use to access methods on this class for cases where I don't know the type of T at runtime. ...

Requirements for collection class to be used with LINQ

I always thought that enough requirement that class should satisfy to be able to use Where() with it is to implement IEnumerable. But today a friend of mine asked me a question, why he cannot apply Where() to the object of SPUserCollection class (it is from Sharepoint). Since this class is derived from SPBaseCollection which implements ...

Split C# collection into equal parts, maintaining sort

I am trying to split a collection into multiple collections while maintaining a sort I have on the collection. I have tried using the following extension method, but it breaks them incorrectly. Basically, if I was to look at the items in the collection, the order should be the same when compared to the broken up collections joined. He...

Casting IEnumberable<T> back to original type with lambda expression

If I have the following how to do I cast the result of the lambda expression back to the Customer type from IEnumerable without having to iterate over it. public class Customer : CustomerModel { public List<Customer> CustomerList {get;set;} public Customer GetCustomerFromListById(long id) { retur...

How to return a resource string at random?

ResourceSet rs = Resources.Loading.ResourceManager.GetResourceSet(CultureInfo.CurrentCulture, true, true); so far I have that line, which gets me all the loading messages, my problem is the ResourceSet is an IEnumerable. I can't figure out what the best way to return a random string in this enumerable would be. Ideally I'd do somethi...

Ienumerable Extension method for dictionary array calls

Hi All Im trying to get an enumberable collection from a dictionary held array. Or should I say, I'm trying to write an extension method for my dictionary objects which store arrays to return an IEnumerable item when the result is null. Im using dictionarys to store array datasets (there are speed reasons for this), which I extract at ...

How to retrieve the generic type used in a generic IEnumerable in .net?

We have a DAL that we're using with NHibernate.Search, so classes that need to be indexed are decorated with an attribute Indexed(Index:="ClassName"), and each property that needs to be be indexed has an attribute Field(Index:=Index.Tokenized, Store:=Store.No) . When one wants the index to drill down special objects, there's the attribut...

How to query a DataSet and set the result as a DataSource for some control? (C# winforms)

I'm digging in in my Microsoft Visual Studio Documentation and I found this article under C# Reference (ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/dv_csref/html/df01e266-5781-4aaa-80c4-67cf28ea093f.htm), It's about Interface Interface. Here's the example code: class SelectSample1 { static void Main() { //Crea...