您好,我正在尝试将Nullable<Double>
属性的列表强制转换为object[][]
属性,如下所示。
WeibullLinearRegression.LinearRegressionPoints = await context.WeibullLinearRegressionPoints.Where(x => x.WeibullFilterDetailsId == filterId).Select(x => new object[]
{ x.XCoordinate !=null? (double)x.XCoordinate : default(double),
x.YCoordinate != null ? (double)x.YCoordinate: default(double)
}
).ToArrayAsync(token);
我尝试了所有可能的组合,使用默认值等,但仍然引发如下错误。不知道为什么会这样。
Unable to cast System.Double to System.Object. LINQ to Entities only supports casting EDM primitive or enumeration types.
非常感谢您的帮助!
更新
其他一些帖子建议尝试使用泛型数组,但仍然无法正常工作! :(
WeibullLinearRegression.LinearRegressionPoints = await context.WeibullLinearRegressionPoints.Where(x => x.WeibullFilterDetailsId == filterId).Select(x => **new []**
{ x.XCoordinate !=null? (double)x.XCoordinate : default(double),
x.YCoordinate != null ? (double)x.YCoordinate: default(double)
}
).ToArrayAsync(token);
答案 0 :(得分:1)
我对您要执行的操作有些迷惑,尤其是对于二维数组。而且,我只是使用对象列表来完成此操作,而不是使用EF等等。因此,我创建了这种类型(抱歉,属性名称较短):
public class DataClass
{
public int Id { get; set; }
public double? X { get; set; }
public double? Y { get; set; }
public DataClass (int id, double? x, double? y)
{
Id = id;
X = x;
Y = y;
}
}
然后我填写了其中的一个列表:
var data = new List<DataClass>
{
new DataClass(1, 1.0, 2.0),
new DataClass(2, 2.0, 3.0),
new DataClass(3, 3.0, null),
};
然后我执行此查询(使用Linq语法,而不是扩展方法):
var result = from d in data
where d.Id > 1
select new[]
{
new
{
X = d.X ?? default(double),
Y = d.Y ?? default(double)
}
};
var array = result.ToArray();
结果是匿名类型的二维数组,但该数组为[2] [1]。在监视窗口中,它看起来像(对不起格式,但是我不知道如何使它看起来更好):
- array {<>f__AnonymousType0<double, double>[2][]} <>f__AnonymousType0<double, double>[][]
- [0] {<>f__AnonymousType0<double, double>[1]} <>f__AnonymousType0<double, double>[]
+ [0] { X = 2, Y = 3 } <Anonymous Type>
- [1] {<>f__AnonymousType0<double, double>[1]} <>f__AnonymousType0<double, double>[]
+ [0] { X = 3, Y = 0 } <Anonymous Type>
那是你想做什么?
好的,我又盯着你的原始帖子,发现你想要一个数组[N] [2],其中两个成员分别是X和Y元素。因此,我尝试了以下方法:
var result2 = from d in data
where d.Id > 1
select new[]
{
d.X ?? default(double),
d.Y ?? default(double)
};
var array2 = result2.ToArray();
并最终在监视窗口中完成此操作
- array2 {double[2][]} double[][]
- [0] {double[2]} double[]
[0] 2 double
[1] 3 double
- [1] {double[2]} double[]
[0] 3 double
[1] 0 double
我认为可能会更接近。它可以与EF一起使用,但我还没有尝试过。
答案 1 :(得分:1)
如以上评论中所述。问题是查询数据库很复杂。
解决方案-转换为ToList,然后调整结果形状。
感谢@DaveM和@ Flydog57
以下是有效的查询。
(await context.WeibullLinearRegressionPoints.Where(x => x.WeibullFilterDetailsId == filterId).ToListAsync(token)).Select(x => new object[] { x.XCoordinate, x.YCoordinate }).ToArray()