8

Hi I'm trying to append 1 list to another. I've done it using AddRange() before but it doesn't seem to be working here... Here's the code:

IList<E> resultCollection = ((IRepository<E, C>)this).SelectAll(columnName, maxId - startId + 1, startId);                
IList<E> resultCollection2 = ((IRepository<E, C>)this).SelectAll(columnName, endId - minId + 1, minId);
resultCollection.ToList().AddRange(resultCollection2);

I did debugging to check the results, here's what I got: resultCollection has a count of 4 resultCollection2 has a count of 6, and after adding the range, resultCollection still only has a count of 4, when it should have a count of 10.

Can anyone see what I'm doing wrong? Any help is appreciated.

Thanks,
Matt

Matt
  • 5,547
  • 23
  • 82
  • 121

5 Answers5

31

When you call ToList() you aren't wrapping the collection in a List<T> you're creating a new List<T> with the same items in it. So what you're effectively doing here is creating a new list, adding the items to it, and then throwing the list away.

You'd need to do something like:

List<E> merged = new List<E>();
merged.AddRange(resultCollection);
merged.AddRange(resultCollection2);

Alternatively, if you're using C# 3.0, simply use Concat, e.g.

resultCollection.Concat(resultCollection2); // and optionally .ToList()
Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
Greg Beech
  • 133,383
  • 43
  • 204
  • 250
4

I would assume .ToList() is creating a new collection. Therefore your items are being added to a new collection that is immediately thrown away and the original remains untouched.

Quibblesome
  • 25,225
  • 10
  • 61
  • 100
  • If I try making it get returned into a new list it says. "cannot implicitly convert type 'void' to 'System.Collection.Generic.List'" so my guess is it's not returning anything? – Matt Nov 12 '09 at 19:16
  • The same mistake I made! AddRange returns void. – Philip Wallace Nov 12 '09 at 19:18
  • 1
    Use the solution offered by Greg Beech. Also, if accepting go for him rather than me, i've only pointed out the problem, he's done that plus provided a solution! :) – Quibblesome Nov 12 '09 at 19:18
1

resultCollection.ToList() will return a new list.

Try:

List<E> list = resultCollection.ToList();
list.AddRange(resultCollection2);
Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
Philip Wallace
  • 7,905
  • 3
  • 28
  • 40
1

Try

IList newList = resultCollection.ToList().AddRange(resultCollection2);

List<E> newList = resultCollection.ToList();
newList.AddRange(resultCollection2);
Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
0

You can use any of the following:

List<E> list = resultCollection as List<E>;
if (list == null)
    list = new List<E>(resultCollection);
list.AddRange(resultCollection2);

Or:

// Edit: this one could be done with LINQ, but there's no reason to limit
//       yourself to .NET 3.5 when this is just as short.
List<E> list = new List<E>(resultCollection);
list.AddRange(resultCollection2);

Or:

List<E> list = new List<E>(resultCollection.Concat(resultCollection2));
Sam Harwell
  • 97,721
  • 20
  • 209
  • 280