I would need to get an average from the below linq query but the data field being varchar, i am 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
where tchg.ParentChannelGroupId == model.LoggedChannelGroupId
select ls).ToList();
ls contains Score1, Score2, Score3, Score4. All these are varchar(50) in the database. Also they allow null values too.
If I need to get an average for Score1 and pass that to my model data, how would i do that?
I tried model.AvgScore1 = _districtResult.Select(m => m.Score1).Average().Value. But i get an error while doing so..
you have to convert (in your example)
m.Score1to a numeric type (here is the MSDN documentation for the Average method. You can not run a mathematical function on string data. you should also probably check for null.something along the lines of this: