generic-list

What is the least amount of code needed to update one list with another list?

Suppose I have one list: IList<int> originalList = new List<int>(); originalList.add(1); originalList.add(5); originalList.add(10); And another list... IList<int> newList = new List<int>(); newList.add(1); newList.add(5); newList.add(7); newList.add(11); How can I update originalList so that: If the int appears in newList, ke...

Why does my attempt to trim strings in a List<string> not appear to work?

I tried the following code in LINQPad and got the results given below: List<string> listFromSplit = new List<string>("a, b".Split(",".ToCharArray())).Dump(); listFromSplit.ForEach(delegate(string s) { s.Trim(); }); listFromSplit.Dump(); "a" and " b" so the letter b didn't get the white-space removed as I was expecting...? A...

C# Update combobox bound to generic list

Hello! I have a combobox on my form that is bound to a generic list of string like this: private List<string> mAllianceList = new List<string>(); private void FillAllianceList() { // Add alliance name to member alliance list foreach (Village alliance in alliances) { mAllianceList.Add(alliance.AllianceName); } ...

How do I use System.Data.DataTableExtensions' CopyToDataTable method?

I'd like to create a data table given a List using the CopyToDataTable method available in DataTableExtensions. I've previously asked the question How do I transform a List into a DataSet? and got an exceptional answer from CMS which was to achieve what I wanted by creating an extension public static DataTable ToDataTable<T>(this IEnumer...

How can I sort List<T> based on properties of T?

My Code looks like this : Collection<NameValueCollection> optionInfoCollection = .... List<NameValueCollection> optionInfoList = new List<NameValueCollection>(); optionInfoList = optionInfoCollection.ToList(); if(_isAlphabeticalSoting) Sort optionInfoList I tried optionInfoList.Sort() but it is not working. ...

Generic list in an interface

I am trying to implement an interface class, that contains a list of objects. How can I make the list generic, so that the implementing class defines the type of list: public interface IEntity { Guid EntityID { get; set; } Guid ParentEntityID{ get; set; } Guid RoleId { get; set; } void SetFromEntity(); void Save(); ...

Create custom generic list with bitmapimage

Hi, In my WPF project there is a listbox in which I have to display images and next to each image their text (for example : date the photo was taken, location etc). I have tried creating a generic List but I still can't assign it to the listbox Something like Bscially I have been trying something on this lines. public class LoadImage...

Converting a List of Base type to a List of Inherited Type

I would be certain that this question addresses something that would have been brought up in a previous question, but I was unable to find it. There is a method in a C# class that takes as a parameter a generic List of a Base Class. I need to pass a list of an inherited class and do not know exactly how to do this. I am getting an err...

How to use XMLSerializer with a Castle ActiveRecord containing an IList<T> member

I am trying to use the XMLSerializer with a castle active record class which looks like the following: [ActiveRecord("Model")] public class DataModel : ActiveRecordBase { private IList<Document> documents; [XmlArray("Documents")] public virtual IList<Document> Documents { get { return documents; } set ...

In .Net, how do you convert an ArrayList to a strongly typed generic list without using a foreach?

See the code sample below. I need the ArrayList to be a generic List. ArrayList arrayList = GetArrayListOfInts(); List<int> intList = new List<int>(); //Can this be foreach be condensed into one line? foreach (int number in arrayList) { intList.Add(number); } return intList; ...

Java: Deriving from a generic List/Collection

Hi folks, I have a little problem understanding the Java language public class PhonebookEntryList extends List<PhonebookEntry> { public PhonebookEntryList(String filename) throws IOException { //loadListFromFilename(filename); } public void saveListToFilename(String filename) throws IOException { //Do som...

how to modify items in a Generic List using foreach?

I have the following Generic List which is populated with a list of string: List<string> mylist =new List<string>(); myList.add("string1"); myList.add("string2"); Say I want to add 'test' at the end of each string, how can I do it in a simple way? Intuitively, I tried this which compiles ok: myList.ForEach(s => s = s + "test"...

.Net - When is List<T>.ForEach prefered over a standard foreach loop?

The generic list class has a .ForEach(Action<T> action) method. Now i've done some simple timings of how they both perform and it seems that the generic ForEach is the poorer performer. The (Snippet Compiler Friendly) code is below - public static class timer{ public static long foreachloop = 0; public static long Gforeachloo...

C# - List<T>.Remove() always deletes the first object on the list

Working in Visual Studio 2008 (C#)... I use a List collection to store instances of my custom class (Shift). I want to delete a certain shift from the list by using the Remove method. But List.Remove() always deletes the first item it finds. I've implemented the IComparable interface for my Shift, I thought this would be enough, then...

How can I easily convert DataReader to List<T> ?

I'm having the data in datareader which i want to be converted in list. What is possible simple solutions for this? For e.g.In CustomerEntity class, I'm having customerId & CustomerName properties.If my datareader returns these two column data. then How can i convert it into List <>. ohh amazing not able to write customerEntity in <> ...

C# Multiple Generic List<t> - Combining Them?!

Scenario: I have a generic list of Audits and a generic list of AuditImages. These two lists have been compiled from database tables. As a result of this, ONE AuditImage can have MANY Audits. As you will see below, the classes that the tables map to are joined by a foreign key relationship "ImageID" when they are in the database, howeve...

C# List<double> size vs double[] size

So I just was testing the CLR Profiler from microsoft, and I did a little program that created a List with 1,000,000 doubles in it. I checked the heap, and turns out the List<> size was around 124KB (I don't remember exactly, but it was around that). This really rocked my world, how could it be 124KB if it had 1 million doubles in it? An...

WCF - Generic List (of T)

I have a list of a custom DTO that I am trying to pass across to a WCF service. I am getting the following error: There was an error while trying to serialize parameter tcp://localhost/:oObject. The InnerException message was 'Type 'System.Collections.Generic.List`1[[TEGE.ER.WorkFlowEngine.WFCommon.HeartBeat.HeartBeatDTO, WFCommon, ...

Remove duplicates in the list using linq

I have a class Items with properties (Id, Name, Code, Price). The List of Items is populated with duplicated items. For ex.: 1 Item1 IT00001 $100 2 Item2 IT00002 $200 3 Item3 IT00003 $150 1 Item1 IT00001 $100 3 Item3 IT00003 $150...

DataTemplate.DataType=Collection<Entity> ?

Is there a way to create a data template that handles a list of items? I have Contact.Phones (EntityCollection) and I want the data template to handle the list - add remove edit etc. Is there a way to set the DataType property of the DataTemplate to generic EntityCollection? ...