2013-04-05 39 views
2

我試圖計數沒有。在其中顯示ProductServices和Add Product的行。特定字符串組合的計數行

日誌是:

INFO [ProductServices]添加產品已由gulanand ID爲424(執行上 產品)INFO [ProductServices]添加產品一直 通過gulanand上產品編號爲424執行( )信息 [商品服務]添加產品已被gulanand執行 商品ID 424()信息[商品服務]更新商品已被 執行gulanand商品ID 424()信息 [商品服務]更新產品已執行由gulanand於 產品編號爲424( )INFO [ProductServices]添加產品已被 執行gulanand對產品ID 424()INFO [ProductServices]添加產品已被執行gulanand在 產品ID爲424()信息[ProductServices]添加產品已被 執行通過對產品gulanand ID爲424()

我曾嘗試代碼是:

IEnumerable<string> textLines = 
    Directory.GetFiles(@"C:\Users\karansha\Desktop\Ashish Logs\", "*.*") 
         .Select(filePath => File.ReadAllLines(filePath)) 
         .SelectMany(line => line); 

List<string> users = new List<string>(); 

Regex r = new Regex(@"*ProductServices\sAdd Product"); 
foreach (string Line in textLines) 
{ 
    if (r.IsMatch(Line)) 
    { 
     users.Add(Line); 
    } 
} 
//string[] textLines1 = new List<string>(users).ToArray(); 
int countlines = users.Count(); 
Console.WriteLine("ProductsCreated=" + countlines); 
+1

* *你的代碼不工作? – Arran 2013-04-05 08:51:00

+2

有什麼問題?你確切的問題是什麼? – roqz 2013-04-05 08:51:02

回答

0

這個什麼:(每個文件)

string[] lines = File.ReadAllLines(filePath) 
int count = lines.Count(input => input.Contains("ProductServices") && 
           input.Contains("Add Product")); 
1

這是你想要的嗎?

int count = File.ReadAllLines().Count(x=>x.Contains("MONKEY")); 

Linq is Luvly!