none
IQueryable<T> doesn't have Reverse member? RRS feed

  • Question

  • Hello!

    I am researching this fragment of code:

    Northwind db=new Northwind(@"Data Source=WIN2008;Initial Catalog=Northwind;Integrated Security=SSPI;");
    var custs=
      (from c in db.Customers
       where c.City=="Rio de Janeiro"
       select c)
      .Reverse();
    foreach(var cust in custs)
     Console.WriteLine("{0}", cust.CompanyName);

    and can't understand - why I get

    The query operator 'Reverse' is not supported.

    error for the custs variable?

    Let's dig into the problem:

    1. custs variable is IQueryable<T> type, more specific - System.Linq.IQueryable<nwind.Customer>.
    2. public static class Queryable provides method 'Reverse' for data structures that implement IQueryable<T> - this exactly suit for custs variable

    So? Why LINQ don't want to call Reverse() from Queryable class??

    Saturday, May 23, 2009 10:07 AM

Answers

  • It does call the Reverse method but then the implementation throws that exception, probably because there is no way to translate Reverse() into T-SQL. You can however do

    (from c in db.Customers
    where c.City == "Rio de Janeiro"
    select c).AsEnumerable().Reverse()

    that way it works as the data is pulled from the database with the AsEnumerable() and then Reverse() is not done with T-SQL on the server but with LINQ to objects on the client.
    MVP XML My blog
    • Marked as answer by Smarty Saturday, May 23, 2009 1:21 PM
    Saturday, May 23, 2009 10:47 AM

All replies

  • It does call the Reverse method but then the implementation throws that exception, probably because there is no way to translate Reverse() into T-SQL. You can however do

    (from c in db.Customers
    where c.City == "Rio de Janeiro"
    select c).AsEnumerable().Reverse()

    that way it works as the data is pulled from the database with the AsEnumerable() and then Reverse() is not done with T-SQL on the server but with LINQ to objects on the client.
    MVP XML My blog
    • Marked as answer by Smarty Saturday, May 23, 2009 1:21 PM
    Saturday, May 23, 2009 10:47 AM
  • Just what I was looking for!

    Friday, September 18, 2009 1:54 PM