Hello friends! I've been playing around with the MS Chart class type (System.Windows.Forms.DataVisualization.Charting) and have a situation where I would like to know how to code a query of my data using LINQ. The data I'm playing with is the PowerBall drawings data that is updated each drawing: http://www.powerball.com/powerball/winnums-text.txt
I'm loading a DataTable
from the file above:
public const int MAX_WB = 59;
public const int MAX_PB = 42; // only 39 now, but must have been upto 42 at one time???
public const int MAX_PP = 5;
public void LoadData(string filename)
{
try
{
//first make sure the file exists
if (!File.Exists(filename))
{
throw new FileNotFoundException("The file " + filename + " could not be found");
}
//create a StreamReader and open our text file
using (StreamReader reader = new StreamReader(filename))
{
Filename = filename;
// Using DataSet; structure same as commented code below...
dt = dsPowerBall.Tables["Drawings"];
#if false
// Add data columns
dt.Columns.Add(new DataColumn("DrawDate", typeof(DateTime)));
dt.Columns.Add(new DataColumn("WB1", typeof(int)));
dt.Columns.Add(new DataColumn("WB2", typeof(int)));
dt.Columns.Add(new DataColumn("WB3", typeof(int)));
dt.Columns.Add(new DataColumn("WB4", typeof(int)));
dt.Columns.Add(new DataColumn("WB5", typeof(int)));
dt.Columns.Add(new DataColumn("PB", typeof(int)));
dt.Columns.Add(new DataColumn("PP", typeof(int)));
#endif
dt.Rows.Clear();
string data = reader.ReadToEnd();
string[] rows = data.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
// skip first row of data containing the column names
for (int i=1; i<rows.Count(); i++)
{
bool error = false;
string r = rows[i];
string[] values = r.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
// scrub data...
if (values.Count() < 7)
error = true;
DateTime date = (DateTime)Convert.ToDateTime(values[0]);
int wb1 = (int)Convert.ToInt32(values[1]);
int wb2 = (int)Convert.ToInt32(values[2]);
int wb3 = (int)Convert.ToInt32(values[3]);
int wb4 = (int)Convert.ToInt32(values[4]);
int wb5 = (int)Convert.ToInt32(values[5]);
int pb = (int)Convert.ToInt32(values[6]);
// power play started later in game and not all records have a pp value...
int pp = 0;
if (values.Count() > 7)
pp = (int)Convert.ToInt32(values[7]);
if (date.Date.CompareTo(new DateTime(1997, 11, 5)) < 0
|| date.Date.CompareTo(System.DateTime.Now) > 0)
{
error = true;
}
else if ((wb1 < 1 || wb1 > MAX_WB)
|| (wb2 < 1 || wb2 > MAX_WB)
|| (wb3 < 1 || wb3 > MAX_WB)
|| (wb4 < 1 || wb4 > MAX_WB)
|| (wb5 < 1 || wb5 > MAX_WB))
{
error = true;
}
else if (pb < 1 || pb > MAX_PB)
error = true;
else if (pp < 0 || pp > MAX_PP)
error = true;
if (error)
{
DumpInvalidRecord(values);
continue;
}
DataRow dr = dt.Rows.Add();
dr["DrawDate"] = date;
dr["WB1"] = wb1;
dr["WB2"] = wb2;
dr["WB3"] = wb3;
dr["WB4"] = wb4;
dr["WB5"] = wb5;
dr["PB"] = pb;
dr["PP"] = pp;
}
}
}
catch (FileNotFoundException ex)
{
Console.WriteLine("Exception: {0}\r\n Stack Trace: {1}", ex.Message, ex.StackTrace);
}
catch (Exception ex)
{
Console.WriteLine("Exception: {0}\r\n Stack Trace: {1}", ex.Message, ex.StackTrace);
}
}
What I would like is an example projection of using LINQ to obtain a count, by ball number, of each of the top 10 winning balls ("WB1" THRU "WB5") from the DataTable. In other words, I'd like to create a list of 10 "numbered" balls having the highest/greatest number of hits.
Thanks in advance for the help!