LINQ Lambda query 'select' does not work with oData

I am currently trying to understand some basics with LINQ. I am using LINQPad to query the OData Netflix source.

Source: http://odata.netflix.com/v2/Catalog/

I cannot select individual properties when using a lambda request - the understanding request works fine. I found a piece of code that performs a more complex query using lambdas in a Netflix OData source, and this seems to work fine for returning a single property of an object.

// works fine
var compQuery = from t in Titles
                where t.ReleaseYear == 2007
                select new { t.Name };
compQuery.Dump();   



// fails: "Can only specify query options (orderby, where, take, skip) after last navigation."
var lambdaQuery = Titles
            .Where(t => t.ReleaseYear == 2007)
            .Select(t => t.Name);

lambdaQuery.Dump(); 


// works fine - found on SO.
var lambdaQuery2 = People
    .Expand("TitlesActedIn")
    .Where(p => p.Name == "George Lucas")
    .First()
    .TitlesActedIn.Select(t => t.ShortName);              

lambdaQuery2.Dump(); 

Can someone shed some light on why the main lambda request fails when asked to return one property?

+5
source share
3 answers

- , :

// fails: "Can only specify query options (orderby, where, take, skip) after last navigation."
var lambdaQuery = Titles
            .Where(t => t.ReleaseYear == 2007)
            .Select(t => new { t.Name });

lambdaQuery.Dump(); 
+4

OData - , :

var lambdaQuery = Titles
            .Where(t => t.ReleaseYear == 2007)
            .Select(x=> new { x.Name })
            .AsEnumerable()
            .Select(t => t.Name);

AsEnumerable() Linq-to-Objects ( OData), .

+12

Using the above answers, I did some tests and found interesting things regarding runtime:

// Avg Execution Time: 5 seconds
var query1 = Titles
            .Where(t => t.ReleaseYear == 2007)
            .Select(t => new {t.Name});     
query1.Dump();


// Avg Execution Time: 15 seconds
var query2 = Titles
            .Where(t => t.ReleaseYear == 2007)
            .AsEnumerable()
            .Select(t => t.Name);       
query2.Dump();

Do I understand correctly that in request 1 only the "Name" property is returned? Whereas in query 2, the "AsEnumerable ()" method returns an object with all property values, hence a longer execution time?

0
source

All Articles