【发布时间】:2017-02-27 08:38:20
【问题描述】:
我试图覆盖一个对象EducationData的中值,模型定义如下,
// 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为var类型,包含教育详情的ES查询结果如附图。
我必须通过一些更改来覆盖该中值。我试过的代码段如下。
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>'
请注意:在模型中将属性更改为 int 会影响其他地方的实现。所以在这里做铸造会更好。
我尝试了 ReSharper 建议,但没有任何效果
resultData[0].Median = Convert.ToDouble(updatedMedian);
resultData[0].Median = IEnumerable<double>updatedMedian;
Also changing the property of updatedMedian will affect the calculation steps.
我也尝试过SO as的一些建议,
- LINQ: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<int>' to 'int'
- Linq Query : Cannot convert type IEnumerable<string> to string changing .ToString() to (string)
- Cannot implicitly convert type 'System.Collections.Generic.IEnumerable
在过去的一天半里,我一直在为此苦苦挣扎。任何建议都会非常有帮助。
【问题讨论】:
-
中位数不是
int,而是IEnumerable<double>。您不能将元素添加到 IEnumerable。将其更改为List<double>并使用resultData[0].Median.Add(7.0) -
Median 似乎是一个双精度数组,将其设置为 int 不起作用。
标签: c# c++ .net generics casting