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 the开发者_开发问答path
where UNTIL thepath == "Binn"
select word;
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
.
Use the Enumerable.TakeWhile
extension method. AFAIK, there is no LINQ syntax for this.
var words = thepath.TakeWhile(word => word != "Binn");
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");
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");
精彩评论