你會如何建議在非泛型IQueryable
上使用AsEnumerable
?非泛型AsEnumerable
我不能底層IQueryProvider
使用Cast<T>
或OfType<T>
方法調用AsEnumerable
之前得到一個IQueryable<object>
,因爲這些方法都有自己明確的翻譯,如果我有一個非映射實體使用它們會破壞查詢翻譯(明顯對象未映射)。
現在,我有我自己的擴展方法(下面),但我想知道是否有一種方法內置到框架。
public static IEnumerable AsEnumerable(this IQueryable queryable)
{
foreach (var item in queryable)
{
yield return item;
}
}
因此,上述擴展方法,我現在可以做的事:
IQueryable myQuery = // some query...
// this uses the built in AsEnumerable, but breaks the IQueryable's provider because object is not mapped to the underlying datasource (and cannot be)
var result = myQuery.Cast<object>().AsEnumerable().Select(x => ....);
// this works (and uses the extension method defined above), but I'm wondering if there's a way in the framework to accomplish this
var result = myQuery.AsEnumerable().Cast<object>().Select(x => ...);
嗯......實際上是否會調用Cast或IEnumerable實現的IQueryable實現?也就是說,第二行真的只是一個IEnumerable,還是它實際上仍然是一個IQueryable? –
Jeff
2010-09-27 14:54:02
@Jeff - 請參閱我的編輯 – Greg 2010-09-27 15:00:04
@ JeffN825,編譯器只會知道它是IEnumerable,因此會鏈接到Enumerable.Cast實現。 –
2010-09-27 15:01:02