0

I have one ArrayList in Session, lets say for example [305,306,380].

On submit, user choice other products which i save them in 2nd array, for example [390,305,480,380]

How can i make another three arrays where

  1. All new values [390,480]

  2. All values which are in both lists [305,380]

  3. All values from list1 which are not in list2 [306]

I need this in ASP.NET 4.0 C#

Christofer Eliasson
  • 32,939
  • 7
  • 74
  • 103
Novkovski Stevo Bato
  • 1,013
  • 1
  • 23
  • 56

2 Answers2

4

You can use ArrayList.ToArray() to get arrays against your arraylists. Then using LINQ you can easily get what you want withExcept an Intersect methods, for example

array2.Except(array1)
array1.Except(array2)
array1.Intersect(array2)

Edit: Complete Code

According to your requirement, your code may look-like this;

        ArrayList arrayList1 = new ArrayList(new int[] { 305, 306, 380 });
        ArrayList arrayList2 = new ArrayList(new int[] { 390, 305, 480, 380 });

        int[] array1 = (int[])arrayList1.ToArray(typeof(int));
        int[] array2 = (int[])arrayList2.ToArray(typeof(int));

        //1. All New values
        int[] uniqueInArray2 = array2.Except(array1).ToArray();

        //2. Common values
        int[] commonValues = array1.Intersect(array2).ToArray();

        //3. Values of arrayList1 which are not in arrayList2
        int[] uniqueInArray1 = array1.Except(array2).ToArray();
ABH
  • 3,391
  • 23
  • 26
  • Sorry I was away from my system for a long time. I have added the complete code. Hopefully it will help! – ABH Apr 07 '12 at 18:55
2

Use HashSet the folowing way:

var first = new HashSet<int>();
first.Add(...);

var second = ...;

1. second.ExceptWith(first);
2. first.IntersectWith(second);
3. first.ExceptWith(second);
Pavel Krymets
  • 6,253
  • 1
  • 22
  • 35