Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comapring two List<String> to extract the common items and put it in List

Im having two List items, how can I write a linq query to compare both and extract the common items.(Like List c, as shown below)

List<string> a = {a, b, c, d}
List<string> b = {c, d, e, f}

List<string> c = {c, d}
like image 611
stranger Avatar asked Nov 29 '22 23:11

stranger


1 Answers

Use the LINQ Intersect method.

 var commonItems = a.Intersect(b);

variable commonItems will be a collection of common items from list a and list b , which is ["c","d"]

like image 160
Shyju Avatar answered Dec 10 '22 05:12

Shyju