public static IEnumerable<KeyValuePair<TKey, TValue>> Where<TKey, TValue>(
this SortedDictionary<TKey, TValue> source,
Expression<Func<KeyValuePair<TKey, TValue>, Boolean>> predicate)
	where TKey : class {
// Pouze pro trasovn: zobrazme nalezen predikt.
Trace.WriteLine("** Predikt Where **");
Trace.WriteLine(predicate.ToString());
Trace.WriteLine("** --------------- **");
// Po nvtv stromu vraz kvli predikt musme vdt,
// zdali je nutn provst standardn nebo optimalizovan opertor Where.
bool replaceWithWhereKey = false;
TKey keyToSearch = null;

// Nsledujc vraz dv delegt, kter vrac kopii
// stromu vraz a eliminuje vraz typu
// <item>.Key == <constant>
// kde <item> je instance typu KeyValuePair<TKey,TValue> (element
// v naem slovnku typu SortedDictionary) a <constant> je konstantn 
// hodnota (testovali jsme pouze etzce).
var ReplaceEqual = ExprOp.Visit.Chain(
(self, last, expr) => {
if (expr == null) return null;
// nvtva stromu vraz, zastaven u BinaryExpression
// se srovnnm typu Equal 
switch (expr.NodeType) {
case ExpressionType.Equal:
var b = (BinaryExpression) expr;
TKey _key = null;
MemberExpression memberAccess = null;
// Naten MemberAccess a Constant bez ohledu na pozici
switch (b.Left.NodeType) {
case ExpressionType.MemberAccess:
memberAccess = b.Left as MemberExpression;
break;
case ExpressionType.Constant:
_key = (b.Left as ConstantExpression).Value as TKey;
break;
}
switch (b.Right.NodeType) {
case ExpressionType.MemberAccess:
memberAccess = b.Right as MemberExpression;
break;
case ExpressionType.Constant:
_key = (b.Right as ConstantExpression).Value as TKey;
break;
}
// Zde se bh zastav beze zmn, nebyl-li nalezen 
// pstup memberAccess nebo kl.
if ((memberAccess == null) || (_key == null)) {
		return b;
}
// Pokud pistoupme k vlastnosti Key typu 
// KeyValuePair<TKey,TValue>, meme provst 
// nhradu  n vraz typu BinaryExpression se nahrad 
// konstantn hodnotou odpovdajc hodnot true.
if ((memberAccess.Member.ReflectedType ==
	typeof(KeyValuePair<TKey, TValue>))
&& (memberAccess.Member.Name == "Key")) {
// Nastaven pznaku pro nhradu podmnky where ve 
// volajc funkci (opertor Where)
replaceWithWhereKey = true;
keyToSearch = _key;
return Expression.Constant(true);
}
Console.WriteLine(b.Left.ToString());
return b;
default:
return last(expr);
} // konec bloku switch (expr.NodeType)
} // konec anonymnho delegta
);

// Nsledujc voln eliminuje porovnn 
// <item>.Key == <constant>
// a pmo pistupuje ke slovnku pomoc
// hodnoty <constant>, nalezen (a uloen v promnn) v ppad, e 
// bylo nalezeno uveden porovnn a odstranno.
var querySubstitution = ReplaceEqual(predicate);
if (replaceWithWhereKey) {
Trace.WriteLine("--- NAHRAZEN WHERE ---");
// Pm naten hodnoty pro dan kl 
// pomoc tdy SortedDictionary.
var value = source[keyToSearch];
var item = new KeyValuePair<TKey, TValue>(keyToSearch, value);
if (predicate.Compile()(item)) {
	yield return item;
}
}
else {
// Tradin prchod podmnkou Where se uskuten,
// pokud nebyl vydn optimalizovan pstup.
Trace.WriteLine("--- STANDARDN WHERE ---");
foreach (var item in source) {
if (predicate.Compile()(item)) {
		yield return item;
}
}
}
}
