Consider the following code that wraps (rather than using inheritance for certain reasons) an instance of Dictionary<string, T> and implements IEnumerable and IQueryable so that it can be used with linq queries:
public class LinqTest<T> : IEnumerable<KeyValuePair<string, T>>, IQueryable<KeyValuePair<string, T>> { private Dictionary<string, T> items = default(Dictionary<string, T>); public virtual T this[string key] { get { return this.items.ContainsKey(key) ? this.items[key] : default(T); } set { this.items[key] = value; } } public virtual T this[int index] { get { return this[index.ToString()]; } set { this[index.ToString()] = value; } } public Type ElementType { get { return this.items.AsQueryable().ElementType; } } public Expression Expression { get { return this.items.AsQueryable().Expression; } } public IQueryProvider Provider { get { return this.items.AsQueryable().Provider; } } public IEnumerator<KeyValuePair<string, T>> GetEnumerator() { return this.items.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return this.items.GetEnumerator(); } }
I tested this code as follows:
LinqTest<dynamic> item = new LinqTest<dynamic>(); item["a"] = 45; item["b"] = Guid.NewGuid(); item["c"] = "Hello World"; item["d"] = true; item.Where(o => o.Value.GetType() == typeof(Guid)).ForEach(i => Console.WriteLine(i));
This indicates that o.Value.GetType() == typeof(Guid) cannot be compiled into an expression because it is dynamic .
However, I tested this theory with the following code:
Dictionary<string, dynamic> item = new Dictionary<string, dynamic>(); item["a"] = 45; item["b"] = Guid.NewGuid(); item["c"] = "Hello World"; item["d"] = true; item.Where(o => o.Value.GetType() == typeof(Guid)).ForEach(i => Console.WriteLine(i));
This compiles and runs without ANY errors and contains a dynamic expression ... can someone explain and possibly indicate how I can fix my code?
Note. .ForEach is a non-standard extension method that implements the foreach loop.
source share