2

Is it possible to return a single value and an enumerable collection using LINQ to SQL?

The problem is, I'm trying to do paging across a large recordset. I only want to return 10 rows at a time so I'm using .Skip(20).Take(10) approach.

However I need to know the total number of records so I can show an appropriate page x of y.

Trying to avoid two separate queries.

Thanks

2 Answers 2

4

Don't be afraid of queries. Do both.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm worried that the page count might change between the two queries. Things might look a bit odd.
@Sir Psycho: Then use a TransactionScope.
1

I came across this exact same issue and ended up with

var q = from i in tableName select i;

int total = q.Count();

foreach(var obj in q.Skip(20).Take(10))
{
    ...
}

It really wasn't a problem at all

1 Comment

That is fine, but it executes two queries. Therefore my answer below.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.