Give two string arrays I want to find the first occurrence of a string in A1 with A2. I know I can do it "long hand" but could I use Array.Find() or something like that?
Many thanks
It sounds like you're interested in the intersection, basically. LINQ to the rescue!
var firstCommon = a1.Intersect(a2).FirstOrDefault();
The documentation for Intersect
would suggest this will return the items in the order of a2
:
When the object returned by this method is enumerated,
Intersect
enumerates first, collecting all distinct elements of that sequence. It then enumerates second, marking those elements that occur in both sequences. Finally, the marked elements are yielded in the order in which they were collected.
However, this is demonstrably incorrect. It will actually enumerate second, and then yield results in the order of first... which is what you want here.
See more on this question at Stackoverflow