2010-01-05 52 views
0

我想從文本文件中獲取隨機行數(假設爲2)。從文本文件中獲取隨機行數

例如,

4581511:        50.27:        AT
1223522:        86.99:        AT
7456117:        68.59:        QW
5261789:        39.17:        QW
.....
.....

文本文件

bookNumber      價格   代碼
4581511:         50.27:        AT
7841522:        26.13:        AT
7353532:        96.13:        AT
1223522:        86.99:        AT
8415621:        89.70:        IT
8411442:        82.42:        IT
4555577:        19.14:        IT
7655577:        65.45:        IT
2754831:        35。44:        DR
1364449:        82.47:        DR
4545454:        45.65:        DR
8795457:        78.36:        DR
5261789:        39.17:        QW
7845522:        10.42:        QW
7456117:        68.59:        QW
4346129 :        23.78:        QW

我能走到今天,我歌廳2線,不是隨機的,而是按順序

代碼

static IEnumerable<string> ReadLines(string path) 
    { 

     using (var file = File.OpenText(path)) 
     { 
      string line; 
      while ((line = file.ReadLine()) != null) 
      { 
       if (line.Contains(":")) 
       { 
        yield return line; 
       } 
      } 
     } 
    } 

    public static IEnumerable<string> GetrandomLines() 
    { 

     string filepath = "file location"; 
     var readTextFile = ReadLines(filepath); 
     var codeGroup = readTextFile.GroupBy(line => line.Substring(line.Length - 2)) 
          .Select(g => new 
          { 
          value = g.Key, 
          count = g.Count() 
          }); 


     foreach (var item in codeGroup) 
     { 
      Random randomLineGenerator = new Random(DateTime.Now.Millisecond); 
      var randomLines = (from x in readTextFile 
           where x.Substring(x.Length - 2) == item.value 
           select x).Skip(randomLineGenerator.Next(0, item.count)).Take(2); 
      foreach (var line in randomLines) 
      { 
       yield return line; 
      } 
     } 

    } 

什麼想法?

感謝

回答

1

的Guid版本,隨機值可以是不一樣的。

public static IEnumerable<string> GetrandomLines2(string filePath, int lines) 
{ 
    return ReadLines(filePath) 
     .GroupBy(line => line.Substring(line.Length - 2)) 
     .SelectMany(s => s.OrderBy(g => Guid.NewGuid()).Take(lines)); 
} 
隨機

版本,兩個隨機值可以是相同的。

public static IEnumerable<string> GetTwoRandomLines(string filePath) 
{ 
    var codeGroup = ReadLines(filePath) 
     .GroupBy(line => line.Substring(line.Length - 2)); 

    Random rnd = new Random(DateTime.Now.Millisecond); 

    foreach (var item in codeGroup) 
    { 
     yield return item.Skip(rnd.Next(item.Count())).FirstOrDefault(); 
     yield return item.Skip(rnd.Next(item.Count())).FirstOrDefault(); 
    } 
} 
0

怎麼這樣呢?

public IEnumerable<string> GetRandomLines(string path, int lines) 
    { 
     foreach (var line in File.ReadAllLines(path).OrderBy(s => Guid.NewGuid()).Take(lines)) 
     { 
      yield return line; 
     } 
    } 
+0

這僅適用於第一個代碼工作是AT,何談其他? – NETQuestion 2010-01-05 20:37:55

+0

這應該適用於任何文件中的所有行。 – BFree 2010-01-05 21:06:17

+0

嗨貝麗 我下面運行你的代碼,並返回只有兩行 GetRandomLines(「C:\ ..,2); 我想是每碼2條隨機線(即AT,IT,DR和QW) THX – NETQuestion 2010-01-06 03:59:20

相關問題