Dataset Sorting and Avg issue

Soldato
Joined
13 Dec 2002
Posts
7,646
Location
Manchester City Centre
I've got a problem with a dataset i'm using

basically i have a c# program which refers to a dataset which is read from and written to an xml file

all works fine for recalling, storing and overwriting data

however

when i try to do one of two things, sorting by an integer column, or averaging an integer column, it all goes pear shaped

for the sort it appears to be handling them as strings, i.e. it sorts 600 above 3000 because the first character is higher

it becomes more apparent that this is what it's trying to do when i try to use the compute(avg) function and it fails because it says it's invalid to use the mean() function on objects of type string

the datatypes are all set correctly in my schema, and when i pull them out individually they are fine and come out as integers, so why when i use these two functions of the dataset does it decide to use them as strings?

any ideas on how to make this work would be much appreciated, obviously i could write my own sort and average methods as it pulls the data correctly individually but i would rather not
 
Not familiar with datasets in c# but can the avg and sort functions for the class take comparers? If so write your own one that will cast each element to int before comparing.
 
oneilldo said:
can't you explicityly cast them as integers after reading them.
i am casting them as they're read, but the functions are performed before they are read, within the dataset

eriedor i will look into that
 
literally just solved this
and you were right to suggest using comparers
used the array of datarows that the select function returns
and wrote a comparer<DataRow> for it which compared the relevant column
I am so relieved right now, i just want to go and get drunk, however i have a bit more polishing up to do before i can leave
 
Back
Top Bottom