Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does AsQueryable() work internally?

Maybe it's simple question, but does AsQueryable() have some performance loss?

Generally speaking we are working with RavenDB and we have existing code like this

protected override IQueryable<T> QueryableIndexRawQuery(string rawQuery, int skip = 0, int take = 128, string indexName = null) 
{ 
    var defaultIndexName = !string.IsNullOrWhiteSpace(indexName) ? indexName : string.Format("{0}{1}", typeof(T).Name, IndexPreffix);

    return this.Session.Advanced.DocumentStore.DatabaseCommands.GetIndex(defaultIndexName) != null 
        ? this.Session.Advanced.LuceneQuery<T>(defaultIndexName).Statistics(out this.Statistics).Skip(skip).Take(take).Where(rawQuery).AsQueryable()
        : this.Session.Advanced.LuceneQuery<T>().Statistics(out this.Statistics).Skip(skip).Take(take).Where(rawQuery).AsQueryable();
}

So Where clause return us IDocumentQuery then we try to represent it AsQueryable()

this.Session.Advanced.LuceneQuery<T>(defaultIndexName).Statistics(out this.Statistics).Skip(skip).Take(take).Where(rawQuery)

The question what AsQueryable() eventually does internally? How does it convert it? Answers with in-memory collection examples like List<> will be really useful as well.

Like this:

    var list = new List<string>() { "1", "2", "3" };
    list.AsQueryable();
like image 424
Jevgenij Nekrasov Avatar asked Mar 21 '26 17:03

Jevgenij Nekrasov


1 Answers

.AsQueryable() forces us to evaluate the query in memory, since you are using session.Advanced.LuceneQuery(). This is NOT recommended. You need to use session.Query() if you want to use IQueryable.

like image 104
Ayende Rahien Avatar answered Mar 23 '26 07:03

Ayende Rahien



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!