9661a8802c6d6f7e876c6fc7bae6721b

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?

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 !

486a0111ccaa1779fdae6a6ff7fadf9c

jonnii

June 30, 2008, June 30, 2008 20:16, permalink

1 rating. Login to rate!
1
2
3
List<T> merged = new List<T>(first);
merged.AddRange(second);
return merged;
69714938a17318a96a5d4673da8892ec

Mark Brackett

July 1, 2008, July 01, 2008 07:51, permalink

No rating. Login to rate!

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);
B162fd4731973004112050da10636a39

Keith Henry

July 1, 2008, July 01, 2008 12:14, permalink

No rating. Login to rate!

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);
F5fc919f4497b21e4b59e37bdebf2816

sdfgsdg

July 1, 2008, July 01, 2008 18:59, permalink

1 rating. Login to rate!

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;
        }
Cd40128e044f39d7063b5cfdeace80f6

volothamp

July 2, 2008, July 02, 2008 15:57, permalink

1 rating. Login to rate!

One line!

1
return new List<T>(first).AddRange(second);

Your refactoring





Format Copy from initial code

or Cancel