Consider the following table, with the following columns:
Name Surname Gender Age
If the records are stored in a data table, this could be easily done by:
DataView dv = new DataView(dtSource);
dv.RowFilter = "Gender = 'M'";
int maleAge = 0;
for (int i = 0; i < dv.Count; ++i)
{
maleAge += Convert.ToInt32(dv[3]);
}
double maleAvgAge = maleAge / dv.Count;
dv.RowFilter = "Gender = 'F'";
int femaleAge = 0;
for (int i = 0; i < dv.Count; ++i)
{
femaleAge += Convert.ToInt32(dv[3]);
}
double femaleAvgAge = femaleAge / dv.Count;