while循环执行时使用Tupled List c#

时间:2015-06-26 07:02:39

标签: c# arrays string while-loop tuples

我有这个代码用于将数据插入数据库。我创建了一个由<string, double, string[]>组成的列表,并在嵌套的while循环中向List添加元素。这是代码....

    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篇文件。 我通过在代码中只显示string document//comment来检查它,它可以正常工作,但不能从数据库中读取文档。 不明白为什么会这样。任何建议都将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

要进入while循环,您的line = file.ReadLine()) != null应该是真的。如果您只到达那里一次,我怀疑您的文件中只有一行,因此,无论您的document数组有多少元素,while内的代码只执行一次。

但总的来说,你的while循环代码对我来说没有多大意义。您将在for的第一次迭代中从文件中读取所有文本,然后将永远跳过while循环。如果您打算只读取所有行一次,请在while之前移动for

要进一步改进代码,请查找ReadLinesAddRange页面。

并在集合中找到最大值而不是

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
}