All Questions

Tagged with
Filter by
Sorted by
Tagged with
1716 votes
29 answers
251k views

Why not inherit from List<T>?

When planning out my programs, I often start with a chain of thought like so: A football team is just a list of football players. Therefore, I should represent it with: var football_team = new ...
Superbest's user avatar
  • 26k
1636 votes
24 answers
1.8m views

How to Sort a List<T> by a property in the object

I have a class called Order which has properties such as OrderId, OrderDate, Quantity, and Total. I have a list of this Order class: List<Order> objListOrder = new List<Order>(); ...
Shyju's user avatar
  • 216k
1125 votes
32 answers
678k views

Randomize a List<T>

What is the best way to randomize the order of a generic list in C#? I've got a finite set of 75 numbers in a list I would like to assign a random order to, in order to draw them for a lottery type ...
mirezus's user avatar
  • 14.1k
890 votes
3 answers
992k views

C# List<string> to string with delimiter

Is there a function in C# to quickly convert some collection to string and separate values with delimiter? For example: List<string> names --> string names_together = "John, Anna, Monica"
nan's user avatar
  • 20k
861 votes
11 answers
668k views

IEnumerable vs List - What to Use? How do they work?

I have some doubts over how Enumerators work, and LINQ. Consider these two simple selects: List<Animal> sel = (from animal in Animals join race in Species ...
Axonn's user avatar
  • 10.2k
779 votes
14 answers
861k views

Using LINQ to remove elements from a List<T>

Say that 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<Author>, how can ...
TK.'s user avatar
  • 47.1k
757 votes
30 answers
902k views

How do I clone a generic list in C#?

I have a generic list of objects in C#, and wish to clone the list. The items within the list are cloneable, but there doesn't seem to be an option to do list.Clone(). Is there an easy way around ...
Fiona's user avatar
  • 7,877
637 votes
32 answers
849k views

Remove duplicates from a List<T> in C#

Anyone have a quick method for de-duplicating a generic List in C#?
JC Grubbs's user avatar
  • 39.7k
610 votes
28 answers
562k views

How to remove elements from a generic list while iterating over it?

I am looking for a better pattern for working with a list of elements which each need processed and then depending on the outcome are removed from the list. You can't use .Remove(element) inside a ...
InvertedAcceleration's user avatar
561 votes
18 answers
239k views

List<T> or IList<T> [closed]

Can anyone explain to me why I would want to use IList over List in C#? Related question: Why is it considered bad to expose List<T>
Peanut's user avatar
  • 19.2k
560 votes
14 answers
1.2m views

How to initialize a list of strings (List<string>) with many string values

How is it possible to initialize (with a C# initializer) a list of strings? I have tried with the example below but it's not working. List<string> optionList = new List<string> { "...
Bilgin Kılıç's user avatar
530 votes
5 answers
335k views

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 ...
Ash Machine's user avatar
  • 9,731
508 votes
17 answers
858k views

How can I add an item to a IEnumerable<T> collection?

My question as title above. For example IEnumerable<T> items = new T[]{new T("msg")}; items.ToList().Add(new T("msg2")); but after all it only has 1 item inside. Can we have ...
ldsenow's user avatar
  • 5,965
500 votes
12 answers
478k views

ArrayList vs List<> in C#

What is the difference between ArrayList and List<> in C#? Is it only that List<> has a type while ArrayList doesn't?
scatman's user avatar
  • 14.3k
485 votes
10 answers
796k views

List<T> OrderBy Alphabetical Order

I'm using C# on Framework 3.5. I'm looking to quickly sort a Generic List<T>. For the sake of this example, let's say I have a List of a Person type with a property of lastname. How would I ...
SaaS Developer's user avatar
461 votes
6 answers
1.2m views

Getting a list item by index

I've recently started using c# moving over from Java. I can't seem to find how to get a list item by index. In java to get the first item of the list it would be: list1.get(0); What is the ...
user1909486's user avatar
  • 4,659
409 votes
8 answers
462k views

How to easily initialize a list of Tuples?

I love tuples. They allow you to quickly group relevant information together without having to write a struct or class for it. This is very useful while refactoring very localized code. Initializing a ...
Steven Jeuris's user avatar
403 votes
5 answers
174k views

Flatten List in LINQ

I have a LINQ query which returns IEnumerable<List<int>> but i want to return only List<int> so i want to merge all my record in my IEnumerable<List<int>> to only one ...
Cédric Boivin's user avatar
390 votes
11 answers
412k views

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 ...
Prasad's user avatar
  • 59.2k
379 votes
10 answers
558k views

How do I copy items from list to list without foreach?

How do I transfer the items contained in one List to another in C# without using foreach?
ratty's user avatar
  • 13.3k
360 votes
15 answers
594k views

Merge two (or more) lists into one, in C# .NET

Is it possible to convert two or more lists into one single list, in .NET using C#? For example, public static List<Product> GetAllProducts(int categoryId){ .... } . . . var ...
zekia's user avatar
  • 4,677
357 votes
5 answers
587k views

Converting from IEnumerable to List [duplicate]

I want to convert from IEnumerable<Contact> to List<Contact>. How can I do this?
kartal's user avatar
  • 17.7k
350 votes
14 answers
435k views

List of Timezone IDs for use with FindTimeZoneById() in C#?

Can someone please point me to a complete list of all the timezones referenced by the id expected in TimeZoneInfo.FindTimeZoneById()? I can't find a list anywhere and I've looked through the .NET ...
Thomas's user avatar
  • 3,764
340 votes
11 answers
668k views

Conversion of System.Array to List

Last night I had dream that the following was impossible. But in the same dream, someone from SO told me otherwise. Hence I would like to know if it it possible to convert System.Array to List Array ...
user193276's user avatar
  • 3,971
333 votes
21 answers
382k views

Split a List into smaller lists of N size [duplicate]

I am attempting to split a list into a series of smaller lists. My Problem: My function to split lists doesn't split them into lists of the correct size. It should split them into lists of size 30 ...
sazr's user avatar
  • 25.4k
324 votes
18 answers
595k views

Quickest way to compare two generic lists for differences

What is the quickest (and least resource intensive) to compare two massive (>50.000 items) and as a result have two lists like the ones below: items that show up in the first list but not in the ...
Frank's user avatar
  • 4,009
312 votes
29 answers
612k views

Convert generic List/Enumerable to DataTable?

I have few methods that returns different Generic Lists. Exists in .net any class static method or whatever to convert any list into a datatable? The only thing that i can imagine is use Reflection ...
Josema's user avatar
  • 4,668
307 votes
7 answers
709k views

Getting all file names from a folder using C# [duplicate]

I wanted to know if it is possible to get all the names of text files in a certain folder. For example, I have a folder with the name Maps, and I would like to get the names of all the text files in ...
user2061405's user avatar
  • 3,271
306 votes
15 answers
559k views

Convert a list to a string in C#

How do I convert a list to a string in C#? When I execute toString on a List object, I get: System.Collections.Generic.List`1[System.String]
IAdapter's user avatar
  • 63.3k
294 votes
9 answers
324k views

Compare two List<T> objects for equality, ignoring order [duplicate]

Yet another list-comparing question. List<MyType> list1; List<MyType> list2; I need to check that they both have the same elements, regardless of their position within the list. Each ...
Bruno Teixeira's user avatar
291 votes
8 answers
237k views

Shorter syntax for casting from a List<X> to a List<Y>?

I know it's possible to cast a list of items from one type to another (given that your object has a public static explicit operator method to do the casting) one at a time as follows: List<Y> ...
Jimbo's user avatar
  • 22.7k
291 votes
10 answers
961k views

How to remove item from list in C#?

I have a list stored in resultlist as follows: var resultlist = results.ToList(); It looks something like this: ID FirstName LastName -- --------- -------- 1 Bill Smith 2 John ...
Nate Pet's user avatar
  • 45.2k
291 votes
11 answers
302k views

Remove items from one list in another

I'm trying to figure out how to traverse a generic list of items that I want to remove from another list of items. So let's say I have this as a hypothetical example List<car> list1 = ...
PositiveGuy's user avatar
  • 47.1k
289 votes
4 answers
821k views

Select distinct using linq [duplicate]

I have a class list of class public class LinqTest { public int id { get; set; } public string value { get; set; } } List<LinqTest> myList = new List<LinqTest>(); myList.Add(new LinqTest(...
Anoop Joshi P's user avatar
269 votes
11 answers
392k views

How can I convert comma separated string into a List<int> [duplicate]

string tags = "9,3,12,43,2" List<int> TagIds = tags.Split(','); This doesn't work cause the split method returns a string[]
nacho10f's user avatar
  • 5,846
254 votes
11 answers
295k views

Split string, convert ToList<int>() in one line

I have a string that has numbers string sNumbers = "1,2,3,4,5"; I can split it then convert it to List<int> sNumbers.Split( new[] { ',' } ).ToList<int>(); How can I convert string ...
uzay95's user avatar
  • 16.3k
254 votes
6 answers
660k views

Get a list of distinct values in List

In C#, say I have a class called Note with three string member variables. public class Note { public string Title; public string Author; public string Text; } And I have a list of type ...
Darrel Hoffman's user avatar
250 votes
13 answers
149k views

Convert List<DerivedClass> to List<BaseClass>

While we can inherit from base class/interface, why can't we declare a List<> using same class/interface? interface A { } class B : A { } class C : B { } class Test { static void Main(...
Asad's user avatar
  • 21.7k
246 votes
12 answers
648k views

How can I find the last element in a List<>?

The following is an extract from my code: public class AllIntegerIDs { public AllIntegerIDs() { m_MessageID = 0; m_MessageType = 0; m_ClassID = 0; ...
zack's user avatar
  • 7,215
233 votes
14 answers
386k views

Check if a string contains an element from a list (of strings)

For the following block of code: For I = 0 To listOfStrings.Count - 1 If myString.Contains(lstOfStrings.Item(I)) Then Return True End If Next Return False The output is: Case 1: ...
user57175's user avatar
  • 3,374
228 votes
5 answers
454k views

Linq select objects in list where exists IN (A,B,C)

I have a list of orders. I want to select orders based on a set of order statuses. So essentially select orders where order.StatusCode in ("A", "B", "C") // Filter the orders based on the order ...
MartinS's user avatar
  • 6,164
224 votes
8 answers
413k views

How do I get the list of keys in a Dictionary?

I only want the Keys and not the Values of a Dictionary. I haven't been able to get any code to do this yet. Using another array proved to be too much work as I use remove also. How do I get a List of ...
Athiwat Chunlakhan's user avatar
221 votes
8 answers
204k views

Case-Insensitive List Search

I have a list testList that contains a bunch of strings. I would like to add a new string into the testList only if it doesn't already exist in the list. Therefore, I need to do a case-insensitive ...
Brap's user avatar
  • 2,717
218 votes
6 answers
323k views

How do you concatenate Lists in C#?

If I have: List<string> myList1; List<string> myList2; myList1 = getMeAList(); // Checked myList1, it contains 4 strings myList2 = getMeAnotherList(); // Checked myList2, it contains 6 ...
Matt's user avatar
  • 5,339
213 votes
7 answers
249k views

How can I add to a List's first position? [duplicate]

I just have a List<T> and I would like to add an item to this list but at the first position. MyList.add() adds the item as the last. How can I add it as the first?. Thanks for help!
bAN's user avatar
  • 13.5k
213 votes
6 answers
213k views

How to merge 2 List<T> and removing duplicate values from it in C#

I have two lists List that I need to combine in third list and remove duplicate values from that lists A bit hard to explain, so let me show an example of what the code looks like and what I want as ...
Kiquenet's user avatar
  • 14.7k
211 votes
9 answers
543k views

How to split() a delimited string to a List<String>

I had this code: String[] lineElements; . . . try { using (StreamReader sr = new StreamReader("TestFile.txt")) { String line; while ((...
B. Clay Shannon-B. Crow Raven's user avatar
210 votes
4 answers
188k views

Create an array or List of all dates between two dates [duplicate]

I am generating multi-series graphs with the date along the X-Axis. The problem is that not all of the series in the graph have the same dates in the date range. Meaning that if I choose 1 Feb ...
Andy Evans's user avatar
  • 7,067
209 votes
4 answers
297k views

C# List of objects, how do I get the sum of a property

I have a list of objects. One property of the individual object entry is amount. How do I get the sum of amount? If my list was of type double I may be able to do something like this: double total = ...
Joseph U.'s user avatar
  • 4,507
205 votes
9 answers
531k views

Check if list is empty in C#

I have a generic list object. I need to check if the list is empty. How do I check if a List<T> is empty in C#?
lakshganga's user avatar
  • 2,415

1
2 3 4 5
312