How does method overload resolution work (LINQ Where extension method)?

让人想犯罪 __ 提交于 2019-12-03 13:23:14

Please read until end.

Actually it is because your code has compiler time errors.

Compiler detects correct extension method by looking at your code. In this case it is supposed to take a Test parameter and return bool parameter. Since your linq expression cannot be compiled, correct extension method cannot be detected, and compiler assumes first extension method it found is the one you wanted.

BTW, if you fix error like

var query3 = objectQuery.Where(t => t.Id == 1)

compiler will use

public static IQueryable<T> Where<T>(
       this IQueryable<T> source,
       Expression<Func<T, bool>> predicate
);

Now you should wonder why it skips method on Enumerable. It is because ObjectQuery<T> class directly implements 'IQueryable', but implements IEnumerable<T> because of IQueryable<T>.

you can see object hierarchy below

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!