2014-11-06 70 views
0

爲什麼要將一個字節數組轉換爲可枚舉的float拋出這個錯誤?DbExpression綁定需要輸入表達式

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 requires an input expression with a collection ResultType.\r\nParameter name: input"}

+0

x是圖像實體(從SQL Server數據庫映像表)x.Bytes是一個字節] – user1307149 2014-11-06 23:31:32

回答

0

不能明確投下字節數組的IEnumerable<float>。嘗試使用.Cast<>代替:

BytesToFloat = (IEnumerable<float>)bytes.Cast<float>() 

現在你的問題是你不能叫.Cast <>對數據庫的查詢,所以你應該把它轉換到一個列表,然後跑投。所以,你的函數變成這樣:

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(); 

    } 
} 
+0

拋出了同樣的錯誤。 – user1307149 2014-11-06 23:47:42

+0

立即嘗試,重寫您的功能,使其工作。 – DavidG 2014-11-06 23:56:47