考虑以下代码,它包装(而不是出于特定原因使用继承) Dictionary<string, T>
的实例并实现IEnumerable
和IQueryable
以便它可以与linq查询一起使用:
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();
}
}
我测试了这段代码如下:
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));
//Compiler error: An expression tree may not contain a dynamic operation
这表明o.Value.GetType() == typeof(Guid)
无法编译到表达式中,因为它是dynamic
。
但是,我用以下代码测试了这个理论:
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));
这个编译,运行没有任何错误,并包含一个动态表达式...任何人都可以解释,并可能指出我如何修复我的代码?
注意: .ForEach
是一种实现foreach循环的非标准扩展方法。
答案 0 :(得分:7)
问题是你的类型实现了IQueryable<>
,所以Queryable
方法是通过成员查找选择的 - 所以编译器试图从你的lambda表达式创建一个表达式树...这就是失败的
字典示例成功,因为它使用Enumerable
而不是Queryable
,所以它将lambda表达式转换为委托。
您可以使用AsEnumerable
:
item.AsEnumerable()
.Where(o => o.Value.GetType() == typeof(Guid))
.ForEach(i => Console.WriteLine(i));
目前尚不清楚为什么要实施IQueryable<>
,说实话 - 另一个更简单的选择就是停止这样做。你的源数据只是Dictionary
,因此已经使用LINQ to Objects而不是基于任何可查询的...为什么要引入IQueryable<>
?