1 2 3 4 5 6 7
public static List<T> MergeListCollections<T>(List<T> firstList, List<T> secondList) { List<T> mergedList = new List<T>(); mergedList.InsertRange(0, firstList); mergedList.InsertRange(mergedList.Count, secondList); return mergedList; }
Refactorings
No refactoring yet !
jonnii
June 30, 2008, June 30, 2008 20:16, permalink
1 2 3
List<T> merged = new List<T>(first); merged.AddRange(second); return merged;
Mark Brackett
July 1, 2008, July 01, 2008 07:51, permalink
LINQ has a couple of extension methods that do this for IEnumerable<T>. They're Enumerable.Union (which removes duplicates, so it's fairly expensive) and Enumerable.Concat (which keeps duplicates). You can then take the IEnumerable<T> and pass it into a new List<T> if needed. You do lose the deferred execution at that point though, and I'd imagine it's slightly less performant than writing your own.
If you want to write your own for List<T>:
1. No need to use InsertRange. AddRange will add to the end of the list.
2. I'd new() the merged List with the known capacity - that'll keep it from having to grow (which can be expensive) as you add items.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// C# 3.0, deferred and no dupes using System.Linq; IEnumerable<T> mergedList = firstList.Union(secondList); // If you want dupes (or don't care) IEnumerable<T> mergedList = firstList.Concat(secondList); // If you want a List<T> List<T> mergedList = new List<T>(firstList.Concat(secondList)); // C# 2.0 List<T> mergedList = new List<T>(firstList.Count + secondList.Count); mergedList.AddRange(firstList); mergedList.AddRange(secondList);
Keith Henry
July 1, 2008, July 01, 2008 12:14, permalink
Similar points to Mark really, I think the Union method in Linq does pretty much what I've done here for C# 2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
//in C# 3, your method has already been done for you, but will clear dupes firstList.Union( secondList ); //or if you don't like extension methods Enumerable.Union( firstList, secondList ); //in C# 2 you can still use yield public static IEnumerable<T> MergeLists<T>(List<T> firstList, List<T> secondList) { foreach( item i in firstList ) yield return i; foreach( item i in secondList) yield return i; } //then if you need a list public static List<T> MergeListCollections<T>(List<T> firstList, List<T> secondList) { return new List<T>( MergeLists<T>(firstList, secondList) ); } // C# 1 List<T> mergedList = new List<T>(firstList.Count + secondList.Count); mergedList.AddRange(firstList); mergedList.AddRange(secondList);
sdfgsdg
July 1, 2008, July 01, 2008 18:59, permalink
sadasdgasgd
1 2 3 4 5 6 7 8 9 10 11 12 13
private void GetOwnersForProjectItems() { var owners = (from item in db.Items where item.Version.ProjectID == new Guid(DDLProjects.SelectedValue) select item.User).Distinct(); owners = owners.OrderBy(User => User.Name); DDLOwners.DataSource = owners; DDLOwners.DataTextField = "Name"; DDLOwners.DataValueField = "UserID"; DDLOwners.DataBind(); DDLOwners.Items.Insert(0, new ListItem("Select Owner", "")); DDLOwners.SelectedIndex = 0; }
Toying with some utility methods, I'm curious if there's a better/faster way to merge two generic lists (of the same type) than what I have here. Perhaps a built in method in the List<T> object?