Reputation: 1112
I have a list i.e.
var d = new List<Tuple<string, double>>();
used in following code as under:
var d = new List<Tuple<string, double>>();
while ((line = file.ReadLine()) != null)
{
d.Add(Tuple.Create(line, p.calculate_CS(line, document)));
}
System.IO.StreamWriter fileW = new System.IO.StreamWriter(@"C:\Users\Malik\Desktop\write_research_fields_temp.txt");
foreach (var item in d.OrderByDescending(t => t.Item2))
{
fileW.WriteLine("{0} - {1}", item.Item2, item.Item1);
}
I want to store first element (only string value not double value) of sorted (descending) List in a string i.e. string top_value
Any suggestion are highly appreciated. Thanks
var dd = new List<Tuple<string, double, string>>();
while ((line = file.ReadLine()) != null)
{
dd.Add(Tuple.Create(line, p.calculate_CS(line, document), document));
}
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);
}
This is insertion method to insert data into database.
static void write_To_Database(string document, string research_field)
{
try
{
SqlConnection con = new SqlConnection("Data Source=KHIZER;Initial Catalog=subset_aminer;Integrated Security=True;");
con.Open();
SqlCommand query = con.CreateCommand();
query.CommandText = "select id from sub_aminer_paper where pid between 1 and 500 and DATALENGTH(p_abstract) != 0 and p_abstract = " + document;
SqlDataReader reader = query.ExecuteReader();
reader.Read();
int id = reader.GetInt32(0);
//reader.Close();
query.Parameters.Add("@research_field", SqlDbType.Text);
query.Parameters.Add("@id", SqlDbType.Int);
query.CommandText = "insert into sub_aminer_paper (research_area) values(@research_field) where id = @id";
query.ExecuteNonQuery();
con.Close();
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Executing finally block.");
}
}
Any suggestions?
Upvotes: 2
Views: 7083
Reputation: 66439
Just use a little LINQ to sort the collection and grab the desired result.
This orders by your numerical value (largest on top), then grabs the first string value paired with it:
string top_value = d.OrderByDescending(x => x.Item2)
.Select(x => x.Item1)
.FirstOrDefault();
In your existing code, there's no need to iterate through all the results just to get the first, so you could basically do something like this:
foreach (var item in d.OrderByDescending(t => t.Item2))
{
string top_value = item.Item1;
... // do stuff
break;
}
But that'd be a little silly, so you could eliminate the loop:
var orderedItems = d.OrderByDescending(t => t.Item2);
var largestItem = d.FirstOrDefault(); // covers the case where the list is empty
string top_value = largestItem != null ? largestItem.Item1 : null;
Personally I prefer LINQ, but YMMV. :)
As for storing it in the database, it looks like you lose the value of document
when you create the Tuple. I'd just change the Tuple to store the document too, and modify the LINQ statement slightly:
var d = new List<Tuple<string, double, string>>();
while ((line = file.ReadLine()) != null)
{
d.Add(Tuple.Create(line, p.calculate_CS(line, document), document));
}
var top_value = d.OrderByDescending(x => x.Item2)
.FirstOrDefault();
if (top_value != null)
{
// look up record using top_value.Item3, and then store top_value.Item1
}
Upvotes: 4