3

I have Arraylist which contains all attribute's IDs (list1). Now I have another set of attribute's Ids (list2) which needs to be remove from first ArrayList (list1)

I am at beginner stage as LINQ developer so please suggest proper code snippet

Arraylist attributeIDs; // which contains Ids
int[] ids = { 1, 2, 3, 4 };
var id = ids.Select(s => s);
var sam = attributeIDs.Cast<IEnumerable>().Where(s => id.Contains(Convert.ToInt32(s)));
Arraylist filterAttributDs = Cast<Arraylist>sam;

After above code, I need to transfer output Arraylist to different methods so I need output in Arraylist only Thanks in advance!

Sandeep
  • 5,581
  • 10
  • 42
  • 62
Sameer Wanwey
  • 31
  • 1
  • 2
  • 7
    Do you *really* need to use `ArrayList` rather than a generic collection? – Jon Skeet Jan 03 '13 at 06:56
  • You could have used the "Except" if it was a List. – Sandeep Jan 03 '13 at 07:03
  • Linq is not made to remove something from a enumerable (collection). It is made to create a new enumerable in which you can exclude certain items. It usually (always) results in a new collection. – Maarten Jan 03 '13 at 07:51

4 Answers4

3

Try out the method

var sam = attributeIDs.Cast<IEnumerable>().Where(s => id.Contains(Convert.ToInt32(s)));
ArrayList myArrayList = new ArrayList(sam );

EDIT

int[] ids = { 1, 2, 3, 4 };
//var id = ids.Select(s => s);
List<int> id = ids.OfType<int>().ToList();
var list = attributeIDs.Cast<int>().ToList();
//or try 
//List<int> list = new List<int>(arrayList.ToArray(typeof(int)));
var sam = list.Where(s => id.Contains(s));
//if you want to remove items than , !Contains() rather an Contains()
// var sam = list.Where(s => !id.Contains(s); 
//also try out Intersect or Except instead of this as jon suggestion in comment 
//var sam= attributeIDs.Except(id); for diffence between list
//var sam= attributeIDs.Intersect(id); for common in list


ArrayList myArrayList = new ArrayList(sam );

combine all

check this for : LINQ to SQL in and not in

Community
  • 1
  • 1
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • I think you mean `Cast`, at which point you can remove the `Convert.ToInt32` call. – Jon Skeet Jan 03 '13 at 07:04
  • @JonSkeet - quetion is not that much clear to me but i think ...he wants to convert output of linq qunery in ArrayList so i tried above code – Pranay Rana Jan 03 '13 at 07:06
  • Well it's not clear why you've still got the first part, which definitely won't work. Also, consider `Intersect` and `Except`... – Jon Skeet Jan 03 '13 at 07:18
1
new Arraylist((from id in attributeIDs where !ids.Contains(id) select id).ToList())

Like Jon mentioned though, you should consider just using a simple array or a generic collection. Also note that the above query runs in O(n*m) where n is the number of items in the original list and m is the number of elements in the list that you are trying to remove. You should consider using a HashSet and then using a set difference operation here for better performance.

Ameen
  • 2,576
  • 1
  • 14
  • 17
1

Slight tweaks to your example:

ArrayList attributeIDs = new ArrayList(){ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] ids = { 1, 2, 3, 4 };
var sam = attributeIDs.Cast<int>().Intersect(ids);

A few notes:

  • The Cast casts the underlying item type, not the type of the collection.

  • foo = setOfThings.Select(a => a) <==> setOfThings

  • Intersect means "only select those elements that appear in both sets"

(as mentioned elsewhere, Intersect is non-optimal for large sets of data: consider using an appropriate structure, like a HashSet)

JerKimball
  • 16,584
  • 3
  • 43
  • 55
0

solution to your problem is :

ArrayList firstList =new ArrayList(new int[]{ 1, 2, 3, 7, 8, 9 });

ArrayList secondList =new ArrayList(new int[] { 1, 3 });

var result = from c in firstList.ToArray() where !(from n in secondList.ToArray() select n).Contains(c) select c;

foreach (var temp in result) Console.WriteLine(temp);

OutPut of the above is : 2 7 8 9 i.e. items of secondList are removed from firstList, also result is a ArrayList as you required to get.

Gaurav Rajput
  • 617
  • 5
  • 8