开发者

Why Skip and Take does not work when passing through a method?

开发者 https://www.devze.com 2023-01-20 15:01 出处:网络
Suppose following codes: IEnumerable<MyClass> MakeQuery() { var query = from m in session.Linq<MyClass>()

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.


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()

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号