Using LINQ to find a common prefix?

Posted by Roger Lipscombe on Stack Overflow See other posts from Stack Overflow or by Roger Lipscombe
Published on 2009-11-12T09:59:37Z Indexed on 2010/03/29 17:53 UTC
Read the original article Hit count: 346

Filed under:
|

I've got two sequences:

IEnumerable<string> x = new[] { "a", "b", "c" };
IEnumerable<string> y = new[] { "a", "b", "d", "e" };

I'd like to find the common prefix of these two sequences (i.e. "a", "b"). Is there a succinct way to do this in LINQ?

Bear in mind that these aren't really IEnumerable<string>; they're IEnumerable<PathComponent>, where I have an implementation of IEqualityComparer<PathComponent>.

© Stack Overflow or respective owner

Related posts about LINQ

Related posts about linq-to-objects