0

Here is my cases,

ListA "1", "2", "3"
ListB "2", "1", "3"

If ListA == ListB, => it should return true since ListA values are in ListB.

ListA "1", "2", "3"
ListB "1", "2", "3", "4"  

=> it should return false since ListB of 4 is not in ListA.

Can anybody help on this?

5
  • Try to use HashSet instead of Lists Commented Apr 26, 2017 at 15:40
  • What should happen if LISTA "1", "2", "3" and LISTB "2", "1", "3", "3"? Or are you worried about that case? Commented Apr 26, 2017 at 15:44
  • I think you're right @Sinatr. The solution I came up with is already on there using sequenceequal and orderby. Commented Apr 26, 2017 at 15:47
  • @KSib Actually it is invalid case since having duplicate values. Anyway it should return false since ListA and LisB length is not equal. Commented Apr 26, 2017 at 15:51
  • Thanks for the clarification. I think your answer is in the link @Sinatr likely posted Commented Apr 26, 2017 at 15:54

2 Answers 2

3

You need to use Except and then Any

var result = !ListB.Except(ListA).Any();
Sign up to request clarification or add additional context in comments.

3 Comments

This will work assuming no duplicate values ever show up in ListB.
@Ksib: Duplicate will also work fine. Say ListB has an extra 3, still result returns true.
It wasn't a question for you, I was clarifying for cgsabari that this is fine if he doesn't mind duplicates. If he does mind duplicates then this won't work. Actually, it looks like this won't work for his requirements as he commented just now
2

If lists contain only unique values you can use HashSet and SetEquals method

new HashSet(listA).SetEquals(new HashSet(listB));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.