How about writing an IQueryable wrapper like this:
class QueryableWrapper<T> : IQueryable<T>
{
private IQueryable<T> _InnerQueryable;
private bool _HasExecuted;
public QueryableWrapper(IQueryable<T> innerQueryable)
{
_InnerQueryable = innerQueryable;
}
public bool HasExecuted
{
get
{
return _HasExecuted;
}
}
public IEnumerator<T> GetEnumerator()
{
_HasExecuted = true;
return _InnerQueryable.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public Type ElementType
{
get { return _InnerQueryable.ElementType; }
}
public System.Linq.Expressions.Expression Expression
{
get { return _InnerQueryable.Expression; }
}
public IQueryProvider Provider
{
get { return _InnerQueryable.Provider; }
}
}
Then you can use it like this:
var query = new QueryableWrapper<string>(
from str in myDataSource
select str);
Debug.WriteLine("HasExecuted: " + query.HasExecuted.ToString());
foreach (string str in query)
{
Debug.WriteLine(str);
}
Debug.WriteLine("HasExecuted: " + query.HasExecuted.ToString());
Output is:
False
String0
String1
...
True