【发布时间】:2013-08-07 21:20:44
【问题描述】:
关于 LINQ 查询语法...
var foo = new List<int> { 1, 2 };
var boo = from n in foo
where n > 1
select n;
...我一直以为这个语法是limited to operating on IEnumerable。或者至少在我了解 IQueryable 之前。也许还有 IObservable。但我最近注意到query syntax is based on duck typing 的建议。直到我找到了一个专门针对LINQ to Tasks 的网站,这个故事看起来并不十分令人信服。 LINQ to Tasks 看起来完全依赖于 duck typing 的查询语法!
好的,这里发生了什么?查询语法是否使用鸭子类型?当我自己试一试时,果然可行,而且似乎证明了这完全是关于鸭子类型的,而不是 IEnumerable:
public class Joker<T>
{
public T Item;
public Joker(T item)
{
Item = item;
}
}
public static class JokerHelp
{
public static T2 Select<T,T2>(this Joker<T> joke, Func<T,T2> call)
{
return call(joke.Item);
}
}
var oof = new Joker<int>(5);
int foo = from a in oof
select a;
如果鸭子类型是查询语法的工作方式,显然是这种情况,那么官方 (MSDN) 文档可能在哪里?或者任何合理的文件?
【问题讨论】:
标签: c# .net linq duck-typing linq-query-syntax