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?

cgsabari
  • 506
  • 2
  • 7
  • 28
  • Try to use HashSet instead of Lists – Valentin Apr 26 '17 at 15:40
  • What should happen if LISTA "1", "2", "3" and LISTB "2", "1", "3", "3"? Or are you worried about that case? – KSib Apr 26 '17 at 15:44
  • I think you're right @Sinatr. The solution I came up with is already on there using sequenceequal and orderby. – KSib Apr 26 '17 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. – cgsabari Apr 26 '17 at 15:51
  • Thanks for the clarification. I think your answer is in the link @Sinatr likely posted – KSib Apr 26 '17 at 15:54

2 Answers2

3

You need to use Except and then Any

var result = !ListB.Except(ListA).Any();
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
  • This will work assuming no duplicate values ever show up in ListB. – KSib Apr 26 '17 at 15:43
  • @Ksib: Duplicate will also work fine. Say `ListB` has an extra 3, still result returns `true`. – Nikhil Agrawal Apr 26 '17 at 15:49
  • 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 – KSib Apr 26 '17 at 15:52
2

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

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

Valentin
  • 5,380
  • 2
  • 24
  • 38