views:

47

answers:

1

I have an expression tree project which works well apart from when I use the GroupBy statement. For statements such as “Where” and “OrderBy” the following line returns a “System.Linq.OrderedEnumerable”

var result = theQueryableSource.Provider.Execute(newExp);

The following line then works correctly as expected.

var test = ((IEnumerable<T>) result).GetEnumerator();

If I use the “GroupBy” statement, the first line of code returns a “System.Linq.GroupedEnumerable”. When the second line runs I get an exception:

Unable to cast object of type 'System.Linq.GroupedEnumerable3 [EntityItemInterfaceClassLib. ReaderEntityItem,System.Nullable1[System.Int32], EntityItemInterfaceClassLib. ReaderEntityItem]' to type 'System.Collections.Generic.IEnumerable`1[EntityItemInterfaceClassLib. ReaderEntityItem]'.

Can anyone advise on how I can cast the GroupedEnumerable to IEnumerable, or explain what is going wrong?

+1  A: 

What do you expect the T to be in your IEnumerable<T>?

GroupedEnumerable<TSource, TKey, TElement> implements IEnumerable<IGrouping<TKey, TElement>> already. In this case, you would be trying to cast theIEnumerable<IGrouping<TKey, TElement>> back to (presumably) an IEnumerable<TSource>.

GroupedEnumerable<TSource, TKey, TElement, TResult> implements IEnumerable<TResult> already. In this case, you would be trying to cast an IEnumerable<TResult> back to (presumably) an IEnumerable<TSource>.

LukeH