ienumerable

Serializing objects which implement IEnumerable<>, using XmlSerializer

Hi, I'm trying to serialize an object which contains an object which implements IEnumerable<>. The last object also contains an object which implements IEnumerable<>. The objects strucutre is as followed: [Serializable] public class A { public B _b {get; set; } } [Serializable] public class B : IEnumerable<C> { public Lis...

Cast IEnumerable<T> to ObservableCollection<T> without constructor injection the IEnumerable<T>

How can I do that? thats a no go: ObservableCollection obsCol = new ObservableCollection(myIEnumerable); scenario: var query = from c in customers select new Customer() { Products = from p in products where p.Id = c.Id ...

Please explain System.Linq.Enumerable.Where(Func<T, int, bool> predicate)

I can't make any sense of the MSDN documentation for this overload of the Where method that accepts a predicate that has two arguments where the int, supposedly, represents the index of the source element, whatever that means (I thought an enumerable was a sequence and you couldn't see further than the next item, much less do any indexin...

Why doesn't the Controls collection provide all of the IEnumerable methods?

I'm not for sure how the ControlCollection of ASP.Net works, so maybe someone can shed some light on this for me. I recently discovered the magic that is extension methods and Linq. Well, I was very sad to find that this isn't valid syntax var c=Controls.Where(x => x.ID=="Some ID").SingleOrDefault(); However from what I can tell, Co...

How to compare custom generic List<> in C#?

I've 2 lists of class A, A has implemented Equals(object obj) and GetHashCode() this 2 methods working correctly, code is below. class A { public string TEST { get; set; } public override bool Equals(object obj) { return ((A)obj).TEST == this.TEST; } public override int GetHashCode(...

IEnumerable<T>.Contains with predicate

I need just to clarify that given collection contains an element. I can do that via collection.Count(foo => foo.Bar == "Bar") > 0) but it will do the unnecessary job - iterate the whole collection while I need to stop on the first occurrence. But I want to try to use Contains() with a predicate, e.g. foo => foo.Bar == "Bar". Currentl...

query that gets only the records needed to the page using Linq- not fetched the record based on page size

i have used this query to fetch the Record of the Datatable based on the Pagesize. IEnumerable<DataRow> query1 = from row in dt.AsEnumerable().Take(page_size).Skip(offset) select row; Datatable boundTable= query1.CopyToDataTable(); first time when it opens the offset will be =0; it gives 10 records, next time i pass the offset a...

How do I break out of recursive IEnumerable<T> loops using yield break?

I have the following method that works well, except the yield break statement only breaks out of the current enumerator. I understand why this is the case, but I am drawing a blank over how to propogate the yield break up through the recursive stack. private static IEnumerable<Node> FindChildrenById(IEnumerable nodes, string parentT...

Linq returning a IEnumerable<Dictionary<string,string>>

I need to return an IEnumerable of a dynamically created Dictionary. pseudo code: var x = from u in Users select new dictionary<string, string>{ Add("Name",u.Name), Add("LastName",u.LastName) } I've been trying many ways to get the pseudo code example above but no success... I would really appreciate your help. ...

how do i convert a IEnumberable<SubClass> to IEnumerable<ParentClass>

How do i go from (cast?, convert?): IEnumerable<Square> to IEnumerable<Shape> ...

Convert DataTable to IEnumerable<T>

I am trying to convert a DataTable to an IEnumerable. Where T is a custom type I created. I know I can do it by creating a List but I was think there was a slicker way to do it using IEnumerable. Here is what I have now. private IEnumerable<TankReading> ConvertToTankReadings(DataTable dataTable) { var tankReadings = n...

Get a distinct list of ids from IEnumerable<T>

I have an IEnumerable that I want to get all the distinct MaterialIDs. I have code that is working but I was wondering if there is a better way possible using LINQ. Here's the code I have: private IEnumerable<int> GetDistinctMaterialIDs(IEnumerable<TankReading> tankReadings) { var distinctMaterialIDs = new List<int>();...

Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.IList'

I have method : public DzieckoAndOpiekunCollection GetChildAndOpiekunByFirstnameLastname(string firstname, string lastname) { DataTransfer.ChargeInSchoolEntities db = new DataTransfer.ChargeInSchoolEntities(); DzieckoAndOpiekunCollection result = new DzieckoAndOpiekunCollection(); if (firstname == nul...

What is an easy way to append or prepend a single value to an IEnumerable<T>?

I need to prepend a single value to an IEnumerable (in this case, IEnumerable<string[]>). In order to do that, I'm creating a List<T> just to wrap the first value so that I can call Concat: // get headers and data together IEnumerable<string[]> headers = new List<string[]> { GetHeaders() }; var all = headers.Concat(GetData()); Yuc...

When to use each of T[], List<T>, IEnumerable<T>?

I usually find myself doing something like: string[] things = arrayReturningMethod(); int index = things.ToList<string>.FindIndex((s) => s.Equals("FOO")); //do something with index return things.Distinct(); //which returns an IEnumerable<string> and I find all this mixup of types/interface a bit confusing and it tickles my potential p...

If yield return never occurs, is null returned?

The method returns IEnumerable via a yield return statement. If the yield statement never occurs (it's inside conditional logic), will the method return null, or will it return an Enumerable with a count of 0? ...

IEnumerable<T> vs T[]

I just realize that maybe I was mistaken all the time in exposing T[] to my views, instead of IEnumerable<T>. Usually, for this kind of code: foreach (var item in items) {} item should be T[] or IEnumerable<T>? Than, if I need to get the count of the items, would the Array.Count be faster over the IEnumerable<T>.Count()? ...

Create List<T> using GetType()

Is it possible to create a List or IEnumerable using GetType(). // If T is Type of Contact I want to Return List<Contact> Test(typeof(Contact));//return List<Type> public static IEnumerable<T> Test<T>(T t) { return new List<T>(); //return List<Type> } ...

What concrete type does 'yield return' return?

What is the concrete type for this IEnumerable<string>? private IEnumerable<string> GetIEnumerable() { yield return "a"; yield return "a"; yield return "a"; } ...

F# yield! operator - Implementation and possible C# equivalents

I'm currently learning F# and I really love the yield! (yield-bang) operator. Not only for its name but also for what it does of course. The yield! operator basically allows you to yield all elements of a sequence from a sequence expression. This is useful for composing enumerators. Since I regularly encounter big, complicated enumerato...