none
Entity Framework .First() working, .Last() fire System.NotSupportedException RRS feed

  • Question

  • Hi,

    Working in .Net 3.5, console app, when I use the simple request 

    var q1 = dbc.Dico.Where(x => x.Name == "GLAQ1CPT").First().VarID;

    It works. but i I ask for Last :

    var q1 = dbc.Dico.Where(x => x.Name == "GLAQ1CPT").Last().VarID;

    It crash, giving :

    L'exception System.NotSupportedException n'a pas été gérée
      Message=LINQ to Entities ne reconnaît pas la méthode « ef_stedGa2_console.Dico Last[Dico](System.Linq.IQueryable`1[ef_stedGa2_console.Dico]) », et cette dernière ne peut pas être traduite en expression de magasin.
      Source=System.Data.Entity
      StackTrace:
           à System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.DefaultTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)
           à System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)
           à System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
           à System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
           à System.Data.Objects.ELinq.ExpressionConverter.Convert()
           à System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)
           à System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
           à System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
           à System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
           à System.Data.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__2[TResult](IEnumerable`1 sequence)
           à System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot)
           à System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[S](Expression expression)
           à System.Linq.Queryable.Last[TSource](IQueryable`1 source)
           à ef_stedGa2_console.Program.Main(String[] args) dans P:\PLN\Mes documents\Visual Studio 2010\Projects\Test\ef-stedGa2-console\ef-stedGa2-console\Program.cs:ligne 13
           à System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
           à System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           à Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           à System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           à System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           à System.Threading.ThreadHelper.ThreadStart()
      InnerException: 

    Do you have any idea why ?

    Rgds,

    Pascal.

    • Moved by Fred BaoModerator Tuesday, September 16, 2014 1:28 AM Being moved from .NET Class Libraries
    Monday, September 15, 2014 9:58 AM

Answers

  • Hello Pascal,

    With your description, I made a same application with .NET 3.5 and it throws the error message like:

    “linq to entities does not recognize the method last”. Is your error message similar with it?

    If it is and you are working with SQL Server, this is because the First() method could be translated to expression “select Top…” since SQL Server owns it while the SQL Server does not have an expression like “select bottom…” so that the Last() method is not recognized by the SQL Server.

    For using the Last() method, we could use LINQ2Object as:

    var result = db.OrderDetails.ToList().Where(od => od.OrderID == 1).Last();

    However, the way above is not recommended since it would load all data from database to memory which might affect the performance.

    The recommended way is to just order descending and then do a First():

    var result = db.OrderDetails.OrderByDescending(od => od.OrderID == 1).First();

    Best Regards,

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, September 16, 2014 3:05 AM
    Moderator

All replies

  • Hello Pascal,

    With your description, I made a same application with .NET 3.5 and it throws the error message like:

    “linq to entities does not recognize the method last”. Is your error message similar with it?

    If it is and you are working with SQL Server, this is because the First() method could be translated to expression “select Top…” since SQL Server owns it while the SQL Server does not have an expression like “select bottom…” so that the Last() method is not recognized by the SQL Server.

    For using the Last() method, we could use LINQ2Object as:

    var result = db.OrderDetails.ToList().Where(od => od.OrderID == 1).Last();

    However, the way above is not recommended since it would load all data from database to memory which might affect the performance.

    The recommended way is to just order descending and then do a First():

    var result = db.OrderDetails.OrderByDescending(od => od.OrderID == 1).First();

    Best Regards,

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, September 16, 2014 3:05 AM
    Moderator
  • Fred,

     Thanks for your answer,  I did fix it the same way (by the the descending order), but I was not understanding why the Last function was not working. Your explanation of no implementation of Bottom function make full sense. Thanks a lot.

     In the same way You may have a solution in order to get the result of a top 10 query with out retrieve all data from the DB (witch could be very long for Big tables)? 

    Thanks in advance. 

    Rgds Pascal .

    Monday, September 22, 2014 8:14 AM
  • Sorry, found it... use .Take(10)...

    Rgds Pascal.

    Monday, September 22, 2014 8:27 AM