0

I'm trying to figure out how to check if all values match other List values.

Example:

List<string> foo = new List<string>() {"banana","apple","coconut"}
List<string> bar = new List<string>() {"banana","apple","coconut","mango", "lemon"}

if every foo list item exists in bar then return true and if at least one is not in foo list then return false. AND if Example:

List<string> foo = new List<string>() {"banana",}
List<string> bar = new List<string>() {"banana","apple","coconut","mango", "lemon"}

should return true

AND if Example:

List<string> foo = new List<string>() {"banana","melon"}
List<string> bar = new List<string>() {"banana","apple","coconut","mango", "lemon"}

should return false

AND if Example:

List<string> foo = new List<string>() {"banana","apple","coconut","mango", "lemon"}
List<string> bar = new List<string>() {"banana"}

should return false

sory for all the changes. got to do this fast :D

3
  • Welcome. Those are different requirements, I don't think there's a clean way to have an all-in-one solution. What have you tried so far? Commented Dec 23, 2020 at 15:09
  • Have you thought about duplicates? eg. foo containing apple twice but bar only has 1 instance of apple? Commented Dec 23, 2020 at 15:17
  • "and if at least one is not in foo" did you mean "and if at least one is not in bar" Commented Dec 23, 2020 at 15:18

3 Answers 3

2

In your case, I think you can use something like

 var resultBool = foo.All(x => bar.Any(y => x == y));

Here, we are comparing the lists by each value. if the foo has a string value which not present in the bar then the result will be false.

I think this will help you to find a solution.

List<string> foo = new List<string>() { "banana", "apple", "coconut" };
List<string> bar = new List<string>() { "banana", "apple", "coconut", "mango", "lemon" };
//true
var test1 = foo.All(x => bar.Any(y => x == y));

List<string> foo2 = new List<string>() { "banana", };
List<string> bar2 = new List<string>() { "banana", "apple", "coconut", "mango", "lemon" };
//true
var test2 = foo2.All(x => bar2.Any(y => x == y));

List<string> foo3 = new List<string>() { "banana", "melon" };
List<string> bar3 = new List<string>() { "banana", "apple", "coconut", "mango", "lemon" };
//false
var test3 = foo3.All(x => bar3.Any(y => x == y));

List<string> foo4 = new List<string>() { "banana", "apple", "coconut", "mango", "lemon" };
List<string> bar4 = new List<string>() { "banana" };
//false
var test4 = foo4.All(x => bar4.Any(y => x == y));

Happy coding :)

Sign up to request clarification or add additional context in comments.

Comments

1

This is actually fairly straight forward using Intersect()

foo.Intersect(bar).Count() == foo.Count()

7 Comments

yeah. it works for first two, but not quite what i'm looking.
Given the updated requirements this should still be okay
Why is it not what you are looking for. Could you explain? How can we answer if we don't have all the information.
Updated the example with randomizing the list, still seems good to me: dotnetfiddle.net/AyINza
If it works for you, then please consider upvoting and accepting this answer. Thanks
|
0

try with Except

var matching = !foo.Except(bar).ToList().Any();

3 Comments

What if the items are out of order?
Yes, fails on randomizing the order - dotnetfiddle.net/f6tres (You may have to run it multiple times, for Test1RandomOrder to print false instead of true)
Yep works now with randomized list - dotnetfiddle.net/jBL1sd

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.