Simple projection fails


Using LINQPad v4.47.02 (WCF Data Services 5.5), connecting to the NuGet OData feed ( https://nuget.org/api/v2 ), this fails:

Packages.Select(x => x.Id)

NotSupportedException: Individual properties can only be selected from a single resource or as part of a type. Specify a key predicate to restrict the entity set to a single instance or project the property into a named or anonymous type.

In this thread it's explained that this is because the OData protocol doesn't support it, but this is actually purely a client bug. This should be handled by the Expression translator in the library.


mausch wrote Oct 20, 2013 at 12:04 AM

Also, if you add a Take() you get a totally different (and cryptic) exception:

Packages.Select(x => x.Id).Take(20)

InvalidCastException: Unable to cast object of type 'System.Data.Services.Client.NavigationPropertySingletonExpression' to type 'System.Data.Services.Client.ResourceSetExpression'

or if you add a condition you get yet another misleading exception:

.Where(x => x.IsLatestVersion && x.IsAbsoluteLatestVersion)
.Select(x => x.Id)

NotSupportedException: Can only specify query options (orderby, where, take, skip) after last navigation.

Which is incorrect, since replacing the projection in the above expression with an anonymous type works fine.

maocampoq wrote Apr 22 at 9:26 PM

You can work around this issue by using anonymous types

var tempList = Packages.Select(x => new{x.Id});
tempList.Select(i => i.Id);