0

Let's say I have two listviews, called A & B. I fill A with 20 items, and a few minutes later, I also fill B with 20 items. Both lists contain completely different data with no duplicates.

Now, for some reason unbeknownst to us, I get in the crazy mood to add the contents in B to listview A. Of course I can do this using a simple ForEach loop, where I go over every item in B and add it to A.

But this takes time and processing power! Really old systems (think about crappy CE handhelds) will surely beg for mercy! So I got to thinking, what if there was a way to merge the listviewcollection of B with the listviewcollection of A in one go?

Is it possible to merge two listviewcollections without a ForEach loop?

  • What version of .net are you using? How many items are those two lists containing? Does some items in B are already in A and need to be filtered out to avoid duplicates? – Alexandre Rondeau Jun 06 '13 at 11:49
  • I've updated the question a bit to provide the information requested in your questions. –  Jun 06 '13 at 11:53
  • You might not gain any performance, because even using the `AddRange` method can be quite slow. At some level, someone has to add to the collection. I have found that it's often better to do it by hand...get both collections into a friendly format, merge, then clear the original collection and add back your newly merged one. As @allo_man has suggested, there might be other things you want to do, and this way you have total control. – DonBoitnott Jun 06 '13 at 11:56

1 Answers1

1

Something like:

MyLvCollection.Items.AddRange( 
MyOtherLvCollection.Select(wi => new ListViewDataItem( 
    wi.Name, new string[]{wi.ID.ToString()} ) ).ToArray() );

EDIT: If you really want it performed client-side (not a web service etc)... then look into using a CompositeCollection, don't know it's performance but seems like the solution for your dilemma using something like XAML.

khellang
  • 17,550
  • 6
  • 64
  • 84
Paul Zahra
  • 9,522
  • 8
  • 54
  • 76
  • I would be using a syntax like this one, but linq isn't faster than foreach and for 20 items I don't see how a device even a very old CE device would be run to the ground. http://stackoverflow.com/questions/1044236/nested-foreach-vs-lambda-linq-query-performancelinq-to-objects – Alexandre Rondeau Jun 06 '13 at 12:11
  • Are you running a webserver on a CE device? If not why in this context would server side code be affected by the client? – Paul Zahra Jun 06 '13 at 12:14
  • P.S. Have you looked into using a CompositeCollection? http://msdn.microsoft.com/en-us/library/system.windows.data.compositecollection.aspx – Paul Zahra Jun 06 '13 at 12:20