2012-08-04 62 views
1

我有它的返回類型爲轉換IEnumerable的到System.Data.Linq.Table

此代碼不能正常工作的功能。我只是想返回了IEnumerable作爲Linq.Table **

public static System.Data.Linq.Table<Products> GetProducts() 
    { 
     MyContext mc = new MyContext(); 

     Table<Products> ret = null; 
     // Or Table<Products> ret = mc.GetTable<Products>(); 

     ret.AttachAll<Products>(mc.GetTable<Products>() 
            .OrderBy(p => p.ProductID).Take(1)); 

     return ret; 
    } 

現在我想的IEnumerable查詢轉換爲System.Data.Linq.Table。

(你可能會說,我可以改變返回類型,但問題是,這是否是可能的。我可以覆蓋像GetTable()?某些功能)

回答

1

你不能。 Table<TEntity> class爲了提供IQueryable<T> interface實現而存在。在那裏沒有任何實際的項目,它只是DataContext class公開的一個根,以提供一個IQueryable<T>實現,該實現被解釋,轉換爲SQL然後發送到SQL服務器。

如果你有一個IEnumerable<T> interface的實現,你很可能已經有你正在迭代的物化結果集(或者內存中的集合)。執行IQueryable<T>沒有任何好處,因爲沒有什麼可以解釋的;你想要的一切已經在內存中。

也就是說,最好只使用IEnumerable<T>實現中的常規擴展方法/查詢語法,而不是試圖將其放入Table<TEntity>