为什么将一个字节数组转换为ienumerable float会抛出此错误?
public List<myModel> Get()
{
List<myModel> data;
using (var db = new MyEntities())
{
var query = db.Images.OrderBy(x => x.ID).Select(x => new myModel
{
Id = x.ID,
Name = x.Name,
Bytes = x.Bytes,
BytesToFloat = (IEnumerable<float>)x.Bytes
});
data = query.ToList();
}
return data;
}
public class myModel
{
public int Id{ get; set; }
public string Name { get; set; }
public Byte[] Bytes { get; set; }
public IEnumerable<float> BytesToFloat { get; set; }
}
我收到此错误:
{“DbExpressionBinding需要一个带有ResultType集合的输入表达式。\ r \ nParameter name:input”}
答案 0 :(得分:0)
您无法将字节数组显式转换为IEnumerable<float>
。请尝试使用.Cast<>
代替:
BytesToFloat = (IEnumerable<float>)bytes.Cast<float>()
现在你的问题是你不能打电话.Cast&lt;&gt;针对数据库查询,因此您应该将其转换为列表然后运行强制转换。所以你的功能就变成了这个:
public List<myModel> Get()
{
using (var db = new myEntities())
{
return db.Images.OrderBy(x => x.Id==1).ToList()
.Select(x => new myModel
{
Id = x.Id,
Name = x.Name,
Bytes = x.Bytes,
BytesToFloat = (IEnumerable<float>)x.Bytes.Cast<float>()
}).ToList();
}
}