-4

Let say I have,

class A
{
    string[] values;
}

class B
{
    string[] values;
}

and I have 2 object,

var a= new A{ values ={"a","b","c"}}
var b= new A{ values ={"b","c"}}

How do I make sure that all values of B should exist in A.values in efficient way?

2
  • possible duplicate of stackoverflow.com/questions/18921673/… Commented May 14, 2014 at 7:36
  • 2
    It's strange that - as a user with quite a lot questions and answers - you didn't show any effort in researching your question and trying something on your own... Commented May 14, 2014 at 7:38

3 Answers 3

3

Use Enumerable.Except to get unique items from b which don't exist in a (this is a set operation, so its pretty efficient). Then use Enumerable.Any to check if there is no such items. If there is no unique items from b which don't exist in a, then all items in b exist in a:

bool allExist = !b.values.Except(a.values).Any();
Sign up to request clarification or add additional context in comments.

Comments

2

You want to check whether set A contains set B. To do this, you produce the set difference between the two sets using Except, and check whether the difference contains any elements.

var contains = ! b.values.Except(a.values).Any();

These things are easier if you imagine a Venn diagram.

Comments

0

EDITED :

List<string> listA = new List<string>(a);
List<string> listB = new List<string>(b);
var listCommon = listA.Where(listB.Contains);

1 Comment

this will check that at least 1 value in common but OP wants all values

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.