2015-06-26 61 views
0

我有這個代碼插入數據到數據庫。我創建了一個<string, double, string[]>的元組列表,並在嵌套的while循環中將元素添加到List中。下面是代碼....循環執行與Tupled列表c#

System.IO.StreamReader file = new System.IO.StreamReader(@"C:\Users\Malik\Desktop\research_fields.txt"); 


      Program p = new Program(); 

      var dd = new List<Tuple<string, double, string>>(); 

      //string document = "The trie data structure has many properties which make it especially attractive for representing large files of data. These properties include fast retrieval time, quick unsuccessful search determination, and finding the longest match to a given identifier. The main drawback is the space requirement. In this paper the concept of trie compaction is formalized. An exact algorithm for optimal trie compaction and three algorithms for approximate trie compaction are given, and an analysis of the three algorithms is done. The analysis indicate that for actual tries, reductions of around 70 percent in the space required by the uncompacted trie can be expected. The quality of the compaction is shown to be insensitive to the number of nodes, while a more relevant parameter is the alphabet size of the key."; 

      //string[] document = get_Abstract(); 
      string line; 
      try 
      { 
       SqlConnection con = new SqlConnection("Data Source=KHIZER;Initial Catalog=subset_aminer;Integrated Security=True"); 

       con.Open(); 
       SqlCommand query = con.CreateCommand(); 
       query.CommandText = "select p_abstract from sub_aminer_paper where pid between 1 and 500 and DATALENGTH(p_abstract) != 0"; 

       SqlDataReader reader = query.ExecuteReader(); 

       string summary = null; 
       while (reader.Read()) 
       { 
        summary = reader["p_abstract"].ToString(); 

        while ((line = file.ReadLine()) != null) 
        { 
         dd.Add(Tuple.Create(line, p.calculate_CS(line, summary), summary)); 
        } 

        var top_value = dd.OrderByDescending(x => x.Item2).FirstOrDefault(); 

        if (top_value != null) 
        { 
         // look up record using top_value.Item3, and then store top_value.Item1 
         var abstrct = top_value.Item3.ToString(); 
         var r_field = top_value.Item1.ToString(); 

         write_To_Database(abstrct, r_field); 
        } 
       } 

       reader.Close(); 
} 
      catch (Exception e) 
      { 
       Console.WriteLine("Exception: " + e.Message); 
      } 
      finally 
      { 
       Console.WriteLine("Executing finally block."); 

      } 

我一直在使用C#在Visual Studio 2013調試它,我看到的是內裏的聲明while循環即dd.Add(Tuple.Create(line, p.calculate_CS(line, summary), summary));只執行一次,而應該是執行22次如reader.Read()有22個文件長度。 我已經檢查了代碼中只顯示爲//comment的單個string document,它工作正常,但不能從數據庫中讀取文檔。 不明白爲什麼會這樣。任何建議將不勝感激。

+0

Beeecaaause ...'file.ReadLine ()'返回null? – Amit

+0

你沒有看到任何異常? – fred

回答

1

要進入while循環,您的line = file.ReadLine()) != null應該是true。如果你只有一次,我懷疑你的文件中只有一行,因此無論document數組有多少個元素,while中的代碼只會執行一次。

但是,總體而言,您的while循環代碼對我來說沒有多大意義。您將在for的第一次迭代中讀取文件中的所有文本,然後while循環將永久跳過。如果您打算只讀一次所有行,請在for之前移動while

爲了進一步改善您的代碼查找ReadLinesAddRange頁面。

,並找到在colleciton代替

var top_value = dd.OrderByDescending(x => x.Item2).FirstOrDefault(); 

使用Max一個最大值:

var top_value = dd.Max(x => x.Item2); 

更新

var lines = System.IO.File.ReadLines(@"C:\Users\Malik\Desktop\research_fields.txt"); 
while (reader.Read()) 
{ 
    summary = reader["p_abstract"].ToString(); 
    dd.AddRange(lines 
     .Select(line => 
      Tuple.Create(line, p.calculate_CS(line, summary), summary) 
     ) 
    ); 
    // rest of your stuff 
}