score:0

a linq query is executed when it has a underlying collection but it is not a collection. you can materialze a query for example by using tolist() or toarray.

every deferred executed enumerable extension tries first to cast the ienumerable<t>to an ilist<t>(f.e. if it needs an indexer) or to a icollection<t>(f.e. if it needs a count). if it can, it will use the "native" method which does not need to execute the query, otherwise it will enumerate the sequence.

search for the tem deferred on msdn to see whether a method is executexd deferred or immediately. if you inspect the source code(f.e. via ilspy), you can detect deferred executed methods by looking for the yield keyword.

union and except are implemented using deferred execution. so you need a tolist/toarray if you want to persist that result.

here's an example, the implementation of enumerable.count:

icollection<tsource> collection = source as icollection<tsource>;
if (collection != null)
{
    return collection.count;
}
icollection collection2 = source as icollection;
if (collection2 != null)
{
    return collection2.count;
}
int num = 0;
using (ienumerator<tsource> enumerator = source.getenumerator())
{
    while (enumerator.movenext())
    {
        num++;
    }
}
return num;

score:1

answer on your question depends on two options:

  • type of operation: lazy (or deferred) and greedy. lazy operations wouldn't be executed immediatly, and defered until code will start materializing data from your linq source. greedy operations always execute immediatly.
    • example of lazy operations: .union, .except, .where, .select and most of other linq operations
    • greedy are: .tolist, .count .toarray and all operations that materialize data
  • source of data for your linq operations. while you're working with linq to memory all operations (both lazy and greedy) will be executed immediately. usualy linq to external sources of data will execute lazy operations only during materialization.

using this two rules you could anticipate how linq will behave:

  1. .count and .tolist will execute immediatly and materialize data
  2. after .tolist, you'll get collection at memory, and all following operations will be executed immediatly (.count will execute one again)
  3. how will .union and .except behave as lazy or greedy depends on type of your data source. for memory they will be greedy, for sql lazy.

example for linqpad. i have one enumerable and lazy or deferred .where and .select operations on it before and after materializing using greedy .count or .tolist:

void main() 
{ 
    "get enumerable".dump(); 
    var samplesenumerable = getsamples(); 

    "get count on enumerable #1".dump(); 
    samplesenumerable.count().dump(); 

    "get enumerable to list #1".dump();  
    var list = samplesenumerable.tolist();   

    "get count on list #1".dump();   
    list.count().dump(); 

    "get count on list again #2".dump(); 
    list.count().dump(); 

    "get where/select enumerable #1".dump(); 
    samplesenumerable 
        .where(sample => { sample.dump(); return sample.contains("5"); }) 
        .select(sample => { sample.dump(); return sample; })
        .dump(); 

    "get where/select list #1".dump(); 
    list 
        .where(sample => { sample.dump(); return sample.contains("5"); }) 
        .select(sample => { sample.dump(); return sample; })
        .dump(); 
} 

string[] samples = new [] { "data1", "data2", "data3", "data4", "data5" }; 

ienumerable<string> getsamples() 
{ 
    foreach(var sample in samples)  
    { 
        sample.dump(); 
        yield return sample; 
    } 
}

sample output. key points

  1. on not materialized data, every .count and .list are retrieving data again and again

    • get count on enumerable #1
    • get where/select enumerable #1
  2. after materializing data, enumerable will be not retreived any more

    • get enumerable to list #1
    • get count on list #1
    • get count on list again #2
    • get where/select list #1

output:

get enumerable
get count on enumerable #1
data1
data2
data3
data4
data5

5
get enumerable to list #1
data1
data2
data3
data4
data5
get count on list #1

5
get count on list again #2

5
get where/select enumerable #1
data1
data1
data2
data2
data3
data3
data4
data4
data5
data5
data5

data5 

get where/select list #1
data1
data2
data3
data4
data5
data5

data5

score:2

union and except are deferredly executed. read remarks section in documentation for more info.

http://msdn.microsoft.com/en-us/library/bb341731

http://msdn.microsoft.com/en-us/library/bb300779

also, in such situation when you are note sure what's happening under the hood, you can use reflector or any other .net decompiler. it's really helpful.


Related Query