2012-02-24 52 views
0

我想知道是否可以在執行查詢本身的同時對LINQ查詢中返回的每個元素執行操作。改變在LINQ查詢中返回的每個值與查詢相同

例子:

var files = Directory.EnumerateFiles(@"C:\etc", "*.*", SearchOption.AllDirectories) 
        .Where(s => (s.ToLower().EndsWith(".psd")) 
           && 
           new FileInfo(s).Length > 500000); 

發現一套標準中的所有文件,但如果我想說微調「C:\」從返回的每個字符串,可我不知怎麼說,S =後F(S) where子句或將這是每個循環的單獨foreach。

謝謝。

回答

2

的Where語句後,您可以添加此

.Select(s => s.Replace(@"C:\","")); 

,將返回與C字符串:\剝離。

2

是的,你可以使用Select方法來做到這一點:

var files = Directory.EnumerateFiles(/* ... */) 
        .Where(/* ... */) 
        .Select(s => s.StartsWith(@"C:\") ? s.Substring(3) : s); 
1

您希望使用Select條款做投影:

var files = Directory.EnumerateFiles(@"C:\etc", "*.*", SearchOption.AllDirectories) 
    .Where(s => s.ToLower().EndsWith(".psd") && new FileInfo(s).Length > 500000) 
    .Select(s => s.Substring(3));