0

Suppose following codes:

IEnumerable<MyClass> MakeQuery()
{
  var query = from m in session.Linq<MyClass>()
              select m;
  return query;
}

List<MyClass> m1()
{
  return MakeQuery()
    .Skip(10)
    .Take(20)
    .ToList<MyClass>();
}

List<MyClass> m2()
{
  var query = from m in session.Linq<MyClass>()
              select m;

  return query
    .Skip(10)
    .Take(20)
    .ToList<MyClass>();
}

Supposing all queries are same, it seems that in m1(), Skip and Take does not work. Indeed its like they do not exist all.

Why this happens and how can be fixed?

I'm using linq-to-nhibernate and this methods are used for paging. Thanks.

Afshar Mohebi
  • 10,479
  • 17
  • 82
  • 126
  • Sorry ChrisF, I was missing some code, but completed it. – Afshar Mohebi Oct 16 '10 at 17:54
  • Have you double checked that the query returns the same data in both cases? I know it should (from the code you've posted), but it doesn't hurt to check these things to eliminate it as a possible cause. – ChrisF Oct 16 '10 at 17:55
  • @ChrisF, Why do you think they have different results? They are both same queries in `MakeQuery()` and `m2`. I have copy-pasted them from same location. – Afshar Mohebi Oct 16 '10 at 18:31
  • I agree, they shouldn't have different results, but I've had situations myself where code I thought was doing the same thing wasn't when I checked intermediate values. As I said they should be returning the same data, but it doesn't hurt to verify it. – ChrisF Oct 16 '10 at 18:33

1 Answers1

2

Why not use IQueryable for the MakeQuery() method?

IQueryable<MyClass> MakeQuery()
{
  return session.Linq<MyClass>();
}

Not that the actual query makes a lot of sense. But I'll leave that to you.

But this is also the only difference between m1() and m2()

Claus Jørgensen
  • 25,882
  • 9
  • 87
  • 150
  • it magically worked! But how is possible? What's difference between `IEnumerable` and `IQueryable`? – Afshar Mohebi Oct 16 '10 at 19:43
  • 1
    IEnumerable is handled in memory, as where the IQueryable is used to build the query before it's executed. I'm unable to say precisely why it's behaving differently, but it must be related to your concrete data structures. Normally both solutions would work. But considering using the IQueryable is also the most performant solution, I would just stick to it. – Claus Jørgensen Oct 17 '10 at 22:51