OrderBy does not translate to SQL when passing a selector function

When I execute:

var t = db.Table1.OrderBy(x => x.Name).ToList(); 

In the SQL profiler, this is translated SQL:

 SELECT [Extent1].[ID] AS [ID], [Extent1].[Name] AS [Name] FROM [dbo].[Table1] AS [Extent1] ORDER BY [Extent1].[Name] ASC 

It is right.

However, if I pass the OrderBy selector function:

 Func<Table1, string> f = x => x.Name; var t = db.Table1.OrderBy(f).ToList(); 

Translated SQL:

 SELECT [Extent1].[ID] AS [ID], [Extent1].[Name] AS [Name] FROM [dbo].[Table1] AS [Extent1] 

The order is not fully translated.

What is the problem? They are one and the same lambda function, the only difference in the second case, it is first assigned to a variable.

+2
source share
1 answer

In the IQueryable world, you need Expression<Func<TModel, TValue>> as an extension parameter of Func<TModel, TValue> not Func<TModel, TValue>

http://msdn.microsoft.com/en-us/library/system.linq.queryable.orderby

+3
source

All Articles