tags:

views:

94

answers:

4

Using LINQ I'm looking to break down the following path string[], however I'd like to break it up to the point of the Binn folder. Is there a WHERE UNTIL operator in LINQ?

c:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\Binn\sqlservr.exe

What I'd like todo

var words = from word in thepath
where UNTIL thepath == "Binn"
select word;
+4  A: 

First, split the path:

var parts = path.Split('\\');

To get the part up to (but not including) "Binn":

var start = parts.TakeWhile(p => p != "Binn");

To get the part after (and including) "Binn":

var rest = parts.SkipWhile(p => p != "Binn");

You can also use Skip or Take to consume or discard a specific number of items from the sequence.

Though if you just want the filename part of a path, use Path.GetFileName.

Daniel Earwicker
+2  A: 

Use the Enumerable.TakeWhile extension method. AFAIK, there is no LINQ syntax for this.

var words = thepath.TakeWhile(word => word != "Binn");
Marcelo Cantos
Sorted TakeWhile was what I needed, thanks very much works a treat.
wonea
+1  A: 

off the top of my head

        var path = @"c:\ Program Files\ Microsoft SQL Server\ MSSQL10.SQLEXPRESS\ MSSQL\ Binn\ sqlservr.exe";
        var words = path.Split('\\');
        var filteredWords = words.TakeWhile(w => w != "Binn");
hhravn
A: 

You could have used a regex

using System.Text.RegularExpressions;
...
String path = @"c:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\Binn\sqlservr.exe";
path = Regex.Replace(path, @".*\\Binn", "Binn");
Alex Rouillard