2012-04-24 88 views
0

我有以下查詢如何將匿名類型轉換爲實體框架?

  var db = new Entities(); 
      IQueryable<Tbl_RSCRegularSupply> qrySupp = (from cont in db.Tbl_RSC 
                 where cont.ID == contractID 
                 let RegSupp = new 
                 { 
                  RegSupply = from R in cont.Tbl_RSCSupplyPlan 
                     select R.Tbl_RSCRegularSupply 
                 } 

                 select (Tbl_RSCRegularSupply)RegSupp.RegSupply); 


      return qrySupp.AsParallel().ToList(); 

但是隻能執行這個創建以下例外。

無法強制類型'System.Collections.Generic.IEnumerable`1'鍵入'Tbl_RSCRegularSupply'。 LINQ to Entities僅支持投射實體數據模型基元類型。

這是從上面的查詢中獲得List<Tbl_RSCRegularSupply>的一些好方法。

回答

0

問題是RegSupp.RegSupplyIEnumerable<EntitySet<Tbl_RSCRegularSupply>>,所以它不能被轉換爲Tbl_RSCRegularSupply。換句話說,你有一個列表清單,而不是一個項目清單。

我不知道你的要求,但是你可以做

} ... select RegSupp.RegSupply.Selectmany(r => r)); 

其壓平,結果在一個列表中。

+0

它並沒有解決問題。我同意你,它返回的東西就像那個IEnumerable >但它如何可能得到列表 Shamim 2012-04-25 05:06:58

+0

'Selectmany'應該這樣做......? (後跟ToList())。 – 2012-04-25 06:43:28