I am using .Take() to get a fixed number of results.
What is the best way to get the TotalCountBeforeTake
(ie as if I didn't use the .Take())?
Can I get the TotalCountBeforeTake
without running the query twice?
var Results = (from results in db.FindWords(term)
orderby results.word
select results.word).Take(100);
//just to get the total record count
int TotalCountBeforeTake = (from results in db.FindWords(term)
select results.word).Count();
// only showing 100 out of TotalCountBeforeTake results,
// but in order to know the TotalCountBeforeTake I had to run the query twice.
foreach (var result in Results)
{
Console.Write(result.ToString());
}
IEnumerable
in question. If backed by a database, then doing multiple queries will be faster. If it were Linq to Objects though, then writing a while loop would probably be faster. – CulturedIEnumerable<T>
, make sure that what you're getting fromdb.FindWords(term)
is anIQueryable<T>
. – Koval