0

I trying make if Arraylist all values contains in other arraylist (all values)

Arraylist one:

Name
Item

ArrayList two:

Item
Name
Count

Note: the lists are shuffled

I tried this:

foreach (string str1 in arraylistone)
                            {
                                if (arraylisttwo.Contains(str1))
                                {
                                    //return true;
                                }
                            }

But it says true if contains someone element

2
  • 2
    Use List<string>. Don't use ArrayList unless you enjoy making your life harder... Commented Jan 23, 2019 at 19:21
  • I Could have not made a better statement. Thanks @elgonzo Commented Jan 23, 2019 at 19:22

3 Answers 3

2

You could use Enumerable.Intersect.

        var listA = new List<string> { "Name", "Item" };
        var listB = new List<string> { "Name", "Item","Count" };

        var c = listA.Intersect(listB).ToList();
Sign up to request clarification or add additional context in comments.

4 Comments

There is no method for Intersect
you need the using System.Linq; statement
I added and not working my arraylist: public ArrayList arraylistone = new ArrayList();
ArrayList doesn't have .Intersect method. What we are suggesting is to use a List<T> System.Collections.Generic;
1

There are many ways to skin a cat. Here is one using Linq's Join extension method:

var list1 = new[] { "Name",  "Item" };
var list2 = new[] { "Item", "Name", "Count" };
var join = list1.Join(list2, l1 => l1, l2 => l2, (l1, l2) => new { l1, l2 });
var allContained = join.Count() == list1.Count();

Just be careful with comparison of strings. There is an overload that takes an IEqualityComparer<TKey> to allow you to define how the keys are compared.

Update:

As Alex Leo has pointed out, you can use Intersect:

 var allContained = list1.Intersect(list2).Count() == list1.Count();

Much simpler and it also provides an IEqualityComparer<T> overload.

4 Comments

How can I get Join from Linq?
@Slinidy: Add a using statement at the top: using System.Linq;
I added and not working my arraylist: public ArrayList arraylistone = new ArrayList();
You need to cast your ArrayList to an Array first: arraylistone.ToArray().
1

Try something like this

var arrayOne = new string[4];
    arrayOne[0] = "Item1";
    arrayOne[1] = "Item2";
    arrayOne[2] = "Item3";
    arrayOne[3] = "Item4";

var arrayTwo = new string[4];
    arrayTwo[0] = "Item1";
    arrayTwo[1] = "Item2";
    arrayTwo[2] = "Item3";
    arrayTwo[3] = "Item4";

    var allItemIsThere = true;

    foreach (var one in arrayOne)
    {
        allItemIsThere = arrayTwo.Contains(one);
        if(!allItemIsThere)
        { break;}
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.