views:

38

answers:

1

Hey, I have this code here:

ArrayList arrayList = new ArrayList();
arrayList.add("one");
arrayList.add("two");
arrayList.add("three");

List<DataRow> dataList = GetDataList(some params);

Now I want to check if arrayList contains ther elements from dataList. The string is at itemarray[0] in dataList. Is there a nice short code version to do that?

Thanks :-)

+3  A: 

In .NET 3.5 to check if all the elements from one list are contained in another list:

bool result = list.All(x => dataList.Contains(x));

Or you can do it using a combination of Except and Any:

bool result = !list.Except(dataList).Any();

In your example you are using an ArrayList. You should change this to List<object> or List<string> to use these methods. Otherwise you can write arrayList.Cast<object>().

bool result = arrayList.Cast<object>().All(x => dataList.Contains(x));
Mark Byers
Thanks, are you sure about the first one? I only have All for the dataList?
grady
I would strongly advise you not to use ArrayList in new code. Use List<T> instead. See my update.
Mark Byers
Yes, you are right, I changed it to be List<string>, but still, one list has <DataRow>, the other string. Something is still missing...
grady
I made it work by calling All on the dataList and using ItemArray.Thanks!
grady