listt

How to add item to the beginning of List<T>?

I want to add a "Select One" option to a drop down list bound to a List<T>. Once I query for the List<T>, how do I add my initial Item, not part of the data source, as the FIRST element in that List<T> ? I have: // populate ti from data List<MyTypeItem> ti = MyTypeItem.GetTypeItems(); //create initial entry MyT...

List<T> Casting algorithms and performance considerations.

I have the following code class Program { static void Main(string[] args) { List<A> aList = new List<A>(); var aObj = new A(); aObj.Go(aList.Cast<IB>()); } } class A : IB { public void Go(IEnumerable<IB> interfaceList) { foreach (I...

C# using LINQ to remove objects within a List<T>

I have LINQ query such as: var authors = from x in authorsList where x.firstname == "Bob" select x; Given that authorsList is of type List, how can I delete any Author that appears within 'var authors' from authorsList? Note: This is a simplified example for the purposes of the question. ...

C# - Determine if List<T> is dirty?

I am serializing Lists of classes which are my data entities. I have a DataProvider that contains a List. I always modify items directly within the collection. What is the best way of determining if any items in the List have changed? I am using the Compact Framework. My only current idea is to create a hash of the List (if that's po...

Class properties concerning List<T> requests – Class design dilema!

When I need to grab more than one record from table(database), my method populates List of particular class (all of my data access layer methods use List for set based select statements. I Don't use datatable/dataset or xmlDocument approach). So lets suppose next simplified scenario; I have two classes – customer and order with these fie...

C# generic list <T> how to get the type of T?

Hello, I’m working on a reflection project, and now I’m stuck. If I have an object of “myclass” that can hold a List does anyone know how to get the type as in the code below if the property myclass.SomList is empty? List<myclass> myList = dataGenerator.getMyClasses(); lbxObjects.ItemsSource = myList; lbxObjects.SelectionChanged +=...

Export a C# List of Lists to Excel

Using C#, is there a direct way to export a List of Lists (i.e., List<List<T>>) to Excel 2003? I am parsing out large text files and exporting to Excel. Writing one cell at a time creates way too much overhead. I chose to use List<T> so that I would not have to worry about specifying the number of rows or columns. Currently, I wait unt...

XML Serialization of List<T> - XML Root

First question on Stackoverflow (.Net 2.0): So I am trying to return an XML of a List with the following: public XmlDocument GetEntityXml() { StringWriter stringWriter = new StringWriter(); XmlDocument xmlDoc = new XmlDocument(); XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);...

Merge and Update Two Lists in C#

I have two List<T> objects: For example: List 1: ID, Value where Id is populated and value is blank and it contains say IDs from 1 to 10. 1,"" 2,"" ... 10,"" List 2: ID, Value and other attributes all filled with values but this list is a subset of List 1 in terms of IDs. (e.g only 3 items) 2,67 4,90 5,98 What I want is a merged li...

List<T> and GridView binding

Here are a few properties of my class. There is also a public object of another class DNRole private Int32 _IDMarketingCampaign; private String _Name; public Int32 IDMarketingCampaign { get { return _IDMarketingCampaign; } set { _IDMarketingCampaign = value; } } public String Name { g...

Contains() method of List<T>

Precisely which methods in a Class are responsible for the List<T>'s Contains() to operate? I have overloaded == in my class. But it seems to have no effect. ...

C# List<T> OrderBy always returning null...

Here's my setup. public class ItemList : List<Item> { public void Load() {...} public void Save() {...} } Load reads from an XML file to populate the ItemList I then attempt to order the item list by a Priority. This is an int? For the test purposes however all the items have a different value. ItemList itemList = new ItemList...

Linq type conversion problem.

Why is this thing giving the message in the second line (i.e. list convertion)? IEnumerable<Order> MyQuery = from order in dataContext.GetTable<Order>() where order.ID == 1 select new Order() {ID = order.ID, OrderDate=order.OrderDate }; List<Order> list = new List<Order>(M...

DataTable to List<T> conversion

Is there any better way than the following? Particularly, I want to replace Activator with something else. public static List<T> ToList<T>(DataTable dt) { Type type = typeof(T); List<T> list = new List<T>(); foreach (DataRow dr in dt.Rows) { object[] args = new o...

DataTable to List<T> conversion problem

While executing this method: public static List<T> ToList<T>(DataTable dataTable) { Type type = typeof(T); List<T> list = new List<T>(); foreach (DataRow dr in dataTable.Rows) { object[] args = new object[1]; args[0] = dr; list.Ad...

Cannot Add to generic list

Firstly I apologise for the oversimplification, but I have this issue which is driving me crazy. It should work - it's simple code, nothing fancy....... I have this object public class Stuff { private int intStuff; private string strStuff; public Stuff(int StuffID, string Stuff) { intStuff = StuffID; s...

C# List<T> lambda Find then Modify elemnts question

How would I go about doing this with a List using lambda List<Foo> list....// create and add a bunch of Foo int seconds = 100; list.FindAll(x=>(x.Seconds == 0).Seconds = seconds) // yes I know that wont work... In other words, find all of the foo objects that Seconds == 0 and change the value to my local variable... I don't want to...

Generic List<T> as IEnumerable<object>

I'm trying to do cast a List to an IEnumerable, so I can verify that different lists are not null or empty: Suppose myList is a List < T > . Then in the caller code I wanted: Validator.VerifyNotNullOrEmpty(myList as IEnumerable<object>, @"myList", @"Clas...

MVVM/WPF: Using a ObservableCollection<T> as a list in a domain model, is that good/bad ?

I have aggregated models like Customer:Order:Product. As my View is bound to the BillingViewModel which has a Property Customers of type ObservableCollection and ONE customer in this collection has a "list" of orders named ObservableCollection and ONE order in this collection has a "list" of products named ObservableCollection Well ...

C# List<T>.BinarySearch return value when value not found

I am confused about the BinarySearch method of List in case when the item does not exist. I've got List<long> theList = {1, 3, 5, ...}. theList.BInarySearch(0) returns 0, and theList.BInarySearch(3) returns 1, as expected. However, theList.BinarySearch(1) returns -2, and not -1 as I'd expect. The MSDN manual says: "Return value: T...