2

I am trying to override the median value of a object EducationData, The model definition is as follows,

// EducationData model
[DataMember(Name = "DegreeConferred", Order = 1)]
public int DegreeConferred { get; set; }
[DataMember(Name = "NoOfInstitutions", Order = 3)]
public int NoOfInstitutions { get; set; }
[DataMember(Name = "Mean", Order = 6)]
public int Mean { get; set; }
[DataMember(Name = "Median", Order = 7)]
public IEnumerable<double> Median { get; set; }

CachedData is of type var, contains the ES query results of the education details as in the picture attached.

enter image description here

I have to override that median value with some alterations. Code segment I tried is as follows.

var resultData = cachedData.ToArray();
resultData[0].Mean = resultData[0].DegreeConferred / resultData[0].NoOfInstitutions; // calculated mean with no error
var updatedMedian = 7; // say for sample, because there is a need to override this queried value with some modification.
resultData[0].Median = updatedMedian; // Error line: Cannot implicitly convert type 'int' to 'System.Collections.Generic.IEnumerable<double>'

Please Note: Changing the property to int in the model will affect the implementation in further places. So it will be better to do casting here.

I tried with ReSharper suggestion, like, but nothing works

resultData[0].Median = Convert.ToDouble(updatedMedian);
resultData[0].Median = IEnumerable<double>updatedMedian;
Also changing the property of updatedMedian will affect the calculation steps.

I have also tried some of the suggestion of SO as,

  1. LINQ: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<int>' to 'int'
  2. Linq Query : Cannot convert type IEnumerable<string> to string changing .ToString() to (string)
  3. Cannot implicitly convert type 'System.Collections.Generic.IEnumerable

I am struggling with this for past one and half days. Any suggestion would be very helpful.

Community
  • 1
  • 1
Jeya Suriya Muthumari
  • 1,947
  • 3
  • 25
  • 47
  • Median is not `int`,but `IEnumerable`. You can't add elements to a IEnumerable. Change it to `List` and use `resultData[0].Median.Add(7.0)` – Pikoh Oct 18 '16 at 07:28
  • Median seems to be an Array of double, setting it to an int cannot work. – Steve Oct 18 '16 at 07:29

2 Answers2

2

There is no type var, and Median is not an int, but rather an enumerable of double. That means that it (potentially) contains multiple values, not just one.

If you want to replace Median with a single value of 7, you can create an array:

resultData[0].Median = new [] { 7d };
Luaan
  • 62,244
  • 7
  • 97
  • 116
  • Thats impressive! Thanks. but incase i need to pass a variable say num instead of 7 a single constant value. What would be the case ? – Jeya Suriya Muthumari Oct 18 '16 at 07:42
  • @MJSuriya The same, `new [] { num }`. That's assuming `num` is a double, of course - otherwise you'll need to either convert it to double or create an array of double explicitly (`new double[]`). – Luaan Oct 18 '16 at 07:43
2

This would help!

        resultData[0].Median = new List<double>() {updatedMedian};
AnjuRaj
  • 298
  • 1
  • 10