IQueryable与IEnumerable以及多态性,其中对象知道它是哪个子类

问题描述 投票:0回答:1

[当使用IEnumberable明确声明查询类型时,例如

IEnumarable<string> q = 
    from c in db.Customers      
    select c.ContactName;   
var q2 = q.Where(s => s.StartsWith(start)); 
return q2;

查询将使用Enumerable.where。为什么查询不知道它是IQueryable并使用Queryable.where。这似乎与OOP概念相矛盾,在OOP概念中,用父类型声明的子对象应该知道它实际上是子类型,并使用子类型的方法。

c# linq oop ienumerable iqueryable
1个回答
2
投票

.Where上的IEnumerable<T>方法是extension method

因此,它不能是虚拟。只有虚拟方法才能表现出您期望的多态性]

© www.soinside.com 2019 - 2024. All rights reserved.