asp.net - get average from linq query -


i need average below linq query data field being varchar, getting error while doing that.

var _districtresult = (from ls in sessionhandler.currentcontext.lennoxsurveyresponses                                    join ml in sessionhandler.currentcontext.mailinglistentries on ls.surveycode equals ml.surveycode                                    join m in sessionhandler.currentcontext.mailinglists on ml.mailinglistid equals m.mailinglistid                                    join ch in sessionhandler.currentcontext.channels on m.channelid equals ch.channelid                                    join chg in sessionhandler.currentcontext.channelgroups on ch.channelgroupid equals chg.channelgroupid                                    join tchg in sessionhandler.currentcontext.channelgroups on chg.parentchannelgroupid equals tchg.channelgroupid                                    tchg.parentchannelgroupid == model.loggedchannelgroupid                                    select ls).tolist(); 

ls contains score1, score2, score3, score4. these varchar(50) in database. allow null values too.

if need average score1 , pass model data, how that?

i tried model.avgscore1 = _districtresult.select(m => m.score1).average().value. error while doing so..

you have convert (in example) m.score1 numeric type (here msdn documentation average method. can not run mathematical function on string data. should check null.

something along lines of this:

_districtresult.select(m => string.isnullorempty(m.score1) ? 0 : //null, use 0                             double.parse(m.score1)).average()    //to double 

Comments