-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefault.aspx.cs
More file actions
174 lines (153 loc) · 6.6 KB
/
Copy pathDefault.aspx.cs
File metadata and controls
174 lines (153 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
namespace ChallengeRecursiva
{
public partial class Default : System.Web.UI.Page
{
protected static DataTable DataSourceCompleto { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
lblMensaje.Text = "";
CSVTable.DataSource = "";
CSVTable.DataBind();
JovenesUniversitariosCasados.Visible = false;
NombresRiver.Visible = false;
EdadesEquipos.Visible = false;
}
protected void Upload(object sender, EventArgs e)
{
if (FileUploadCSV.PostedFile != null && FileUploadCSV.PostedFile.FileName.Length > 0)
{
if (FileUploadCSV.FileName.EndsWith(".csv"))
{
string path = Server.MapPath("~/" + Path.GetFileName(FileUploadCSV.FileName));
DataSourceCompleto = ConvertCSVtoDataTable(path);
FileUploadCSV.SaveAs(path);
int HinchasRacing = 0;
EnumerableRowCollection<DataRow> EdadesRacing;
Int64 EdadAcumuladaRacing = 0;
if (DataSourceCompleto != null)
{
HinchasRacing = DataSourceCompleto.AsEnumerable().Count(x => x.Field<String>("Equipo") == "Racing");
EdadesRacing = DataSourceCompleto.AsEnumerable().Where(y => y.Field<String>("Equipo") == "Racing");
foreach (var item in EdadesRacing.ToList())
{
EdadAcumuladaRacing += long.Parse(item.ItemArray[1].ToString());
}
}
if (EdadAcumuladaRacing != 0)
{ CantidadTotal.Text = "Cantidad total de personas registradas: " + DataSourceCompleto.Rows.Count; }
else
{ CantidadTotal.Text = ""; }
if (HinchasRacing != 0)
{ PromedioRacing.Text = "Promedio de edad en Racing: " + (EdadAcumuladaRacing / HinchasRacing); }
else
{ PromedioRacing.Text = ""; }
lblMensaje.Text = "\"" + FileUploadCSV.FileName + "\" cargado de forma exitosa.";
JovenesUniversitariosCasados.Visible = true;
NombresRiver.Visible = true;
EdadesEquipos.Visible = true;
}
else
{ lblMensaje.Text = "Tipo de Archivo Incorrecto, vuelva a intentarlo con un .CSV"; }
}
}
protected static DataTable ConvertCSVtoDataTable(string strFilePath)
{
DataTable dt = new DataTable();
using (StreamReader sr = new StreamReader(strFilePath, Encoding.Default, true))
{
string[] headers = "Nombre;Edad;Equipo;Estado Civil;Nivel de Estudios".Split(';');
foreach (string header in headers)
{
dt.Columns.Add(header);
}
while (!sr.EndOfStream)
{
string[] rows = sr.ReadLine().Split(';');
DataRow dr = dt.NewRow();
for (int i = 0; i < headers.Length; i++)
{
dr[i] = rows[i];
}
dt.Rows.Add(dr);
}
}
return dt;
}
protected void JovenesUniversitariosCasados_Click(object sender, EventArgs e)
{
if (DataSourceCompleto != null)
{
DataTable dt = DataSourceCompleto;
for (int i = dt.Rows.Count - 1; i >= 0; i--)
{
DataRow dr = dt.Rows[i];
if (dr[3].ToString() == "Casado" && dr[4].ToString() == "Universitario") { }
else
{ dr.Delete(); }
}
dt.AcceptChanges();
dt.DefaultView.Sort = "Edad ASC";
dt = dt.DefaultView.ToTable();
dt.Columns.Remove("Estado Civil");
dt.Columns.Remove("Nivel de Estudios");
dt = dt.AsEnumerable().Take(100).CopyToDataTable();
CSVTable.DataSource = dt;
CSVTable.DataBind();
}
}
protected void NombresComunesRiver_Click(object sender, EventArgs e)
{
if (DataSourceCompleto != null)
{
DataTable dt = DataSourceCompleto;
for (int i = dt.Rows.Count - 1; i >= 0; i--)
{
DataRow dr = dt.Rows[i];
if (dr[2].ToString() != "River")
{
dr.Delete();
}
}
dt.AcceptChanges();
dt.Columns.Remove("Edad");
dt.Columns.Remove("Equipo");
dt.Columns.Remove("Estado Civil");
dt.Columns.Remove("Nivel de Estudios");
CSVTable.DataSource = dt.AsEnumerable().GroupBy(r => new { Nombre = r.Field<string>("Nombre") }).Select(grp => new { Nombre = grp.Key.Nombre, Cantidad = grp.Count() });
CSVTable.DataBind();
}
}
protected void EdadesPorEquipos_Click(object sender, EventArgs e)
{
if (DataSourceCompleto != null)
{
DataTable dt =
DataSourceCompleto.AsEnumerable()
.GroupBy(r => new { Equipo = r["Equipo"] })
.OrderByDescending(id => id.Count())
.Select(d =>
DataSourceCompleto.Rows.Add
(
d.Key.Equipo,
(int)d.Average(r => int.Parse(r.Field<string>("Edad"))),
d.Min(r => long.Parse(r.Field<string>("Edad"))),
d.Max(r => long.Parse(r.Field<string>("Edad"))))
)
.CopyToDataTable();
dt.Columns["Equipo"].ColumnName = "Edad Minima";
dt.Columns["Nombre"].ColumnName = "Equipo";
dt.Columns["Edad"].ColumnName = "Edad Promedio";
dt.Columns["Estado Civil"].ColumnName = "Edad Maxima";
dt.Columns.Remove("Nivel de Estudios");
CSVTable.DataSource = dt;
CSVTable.DataBind();
}
}
}
}