ienumerable

Serialize IEnumerable<T> through the wire?

Hi, I'm using a distributed web application, with a database server housing the SQL Server 2005 database, an application server where we've installed a Microsoft Three Tier Service Application, and a web server where I using MVP with supervising controller. My service layer, on the application server, returns an IEnumerable<Country> wh...

Nested yield return with IEnumerable

I have the following function to get validation errors for a card. My question relates to dealing with GetErrors. Both methods have the same return type IEnumerable<ErrorInfo>. private static IEnumerable<ErrorInfo> GetErrors(Card card) { var errors = GetMoreErrors(card); foreach (var e in errors) yield return e; /...

Some issues with sending List<T> as IEnumerable<T> to a method.

Possible Duplicate: Upcasting and generic lists Ok, I want to send a List<CardHolder> as an IEnumerable<ICardHolder> where CardHolder : ICardHolder. However, the compiler errors: Error 4 Argument '1': cannot convert from 'System.Collections.Generic.List' to 'System.Collections.Generic.IEnumerable' This seems strange to me...

How to group items by index? C# LINQ

Suppose I have var input = new int[] { 0, 1, 2, 3, 4, 5 }; How do I get them grouped into pairs? var output = new int[][] { new int[] { 0, 1 }, new int[] { 2, 3 }, new int[] { 4, 5 } }; Preferably using LINQ ...

How to get the index of an element in an IEnumerable?

I wrote this: public static class EnumerableExtensions { public static int IndexOf<T>(this IEnumerable<T> obj, T value) { return obj .Select((a, i) => (a.Equals(value)) ? i : -1) .Max(); } public static int IndexOf<T>(this IEnumerable<T> obj, T value , IEqualityComparer<T> comp...

How do I select a subset of items from an anonymous type IEnumerable?

I have the following code. MyDataContext db = MyDataContext.Create(); bc = db.BenefitCodes.Select( b => new { BenCd = b.BenCd , Description = b.BenDesc , BenInter...

Memory usage of iterators in C#

What is the impact on memory usage of using lots of iterators in C#? Let's assume a program that performs thousands of foreach loops -- does each loop allocate a temporary object on the heap via a call to GetEnumerator? Does the CLR perform any kind of optimization (e.g. stack allocation of IEnumerator objects)? Or is this simply not ...

Changing value of one item in a collection affects all duplicate items

I seem to be having an odd issue whereby every time I try to change a value of an item in a collection, it affects all others that contain the same initial values. An example is below: public class Product : ICloneable { public int Id { get; set; } public string Name { get; set; } public int Quantity { get; set; } public Produ...

Replace nested ForEach with Select if applicable

Is it possible to replace method ForEach() usage with Select() or something else to write next code in one string with nested extension methods? OR maybe there are another ways to improve the algorithm? var list = new List<IStatementParser>(); System.IO.Directory.GetFiles(path, "*.dll") .ForEach(f => System.Reflection.Assembly.Load...

IEnumerable list through override chain

Alright, hard to phrase an exact title for this question, but here goes... I have an abstract class called Block, that looks something like this: public abstract class Block { public bool Enabled{get; private set;} public virtual IEnumerable<KeyValuePair<string, string>> GetDefaultUsages() { yield return new KeyValuePair...

Someone please clarify this IEnumerable<T> business for my MVC App?

OK so here we go, one of my classes called Product is implemented like this: namespace DomainModel.Entities { public class Product { public int ProductID { get; set; } public string Name { get; set; } public string Description { get; set; } public decimal Price { get; set; } } } I want to a...

IEnumerable: Whats does it mean in the context of OOP

Please consider the following code: public class Person ( public string FirstName {get; set;} public string LastName {get; set;} Public int Age {get; set;} } IEnumerable <Person> people; Also i have seen in many programs something like <IQueryable> what does that mean ? What is the meaning of IEnumerable<Person> here ?...

C# IEnumerable<Object> to string

Hi, For log purpose, I would like to call the .ToString() method of every object on an object[] array. How can I do this in the simpliest way ? Say I have : myArray = new Object[]{"astring",1, Customer} Log(????); How can I pass un string such as its value is equals to "astring".ToString()+1.ToString()+Customer.ToString() Or bette...

Is yield return in C# thread-safe?

I have the following piece of code: private Dictionary<object, object> items = new Dictionary<object, object>; public IEnumerable<object> Keys { get { foreach (object key in items.Keys) { yield return key; } } } Is this thread-safe? If not do I have to put a lock around the loop or the y...

Update properties of objects in an IEnumerable<>

Hi, I am working on some software that should be used for a special type of experiment. The experiments are performed using: 1) A "Chip" (basically an XY grid of known dimensions). 2) Each Chip contains "Electrodes", identified by their X and Y coordinate on the chip and by a unique ID. Each electrode can also hold or not hold a sampl...

Are there any pitfalls to using an IEnumerable<T> return type for SQL data?

My question is concerning SQL connection status, load, etc. based on the following code: public IEnumberable<MyType> GetMyTypeObjects() { string cmdTxt = "select * from MyObjectTable"; using(SqlConnection conn = new SqlConnection(connString)) { using(SqlCommand cmd = new SqlCommand(cmdTxt, conn)) { conn.Open(); ...

Need Help on Looping Through Deserialized Custom Class

I am having a hard time figuring out how to expose (& loop through) the properties of my Categories class which was serialized (using JSON) in a WCF service and deserialized on the server as illustrated below. JavaScriptSerializer serializer = new JavaScriptSerializer(); Category cat = serializer.Deserialize<Category>(param1); // M...

Why can't I foreach through my ViewData or Model?

I keep getting errors trying to iterate through my ViewData in the view there... I even tried strongly typing the view to IEnumerable(App.Models.Namespace) and using Model, to no avail. Either I get an error for lack of a GetEnumerable method or invalid type casting... Any idea how I do this? Model... public IQueryable<Product> getAllP...

Enumerable.Unit() function

Finding myself in the situation where I have a method with this signature void DoSomething(IEnumerable<T> before, IEnumerable<T> after) I find myself often having to call it when I've just got one element and not IEnumerable. I thought of adding the three overloads, but that doesn't help when one of the arguments is null. So I thoug...

LINQ-to-List and IEnumerable issues

I am querying an HTML file with LINQ-to-XML. It looks something like this: <html> <body> <div class="Players"> <div class="role">Goalies</div> <div class="name">John Smith</div> <div class="name">Shawn Xie</div> <div class="role">Right Wings</div> <div class="name">Jack Davis</div> ...