Reputation: 31
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!
Upvotes: 3
Views: 3459
Reputation: 647
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.
Upvotes: 0
Reputation: 176906
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
Upvotes: 3
Reputation: 16894
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)
Upvotes: 1
Reputation: 2586
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.
Upvotes: 1