LINQ where子句使用了func<>代表 [英] LINQ where clause used func<> delegate
本文介绍了LINQ where子句使用了func<>代表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨专家,
我在LINQ中有一个问题。
当我们在LINQ中使用where子句时,它使用Func< tsource>代表。
Hi Experts,
I have one question in LINQ.
When we use where clause in LINQ it use Func<tsource> delegate.
List<int> numbers = new List<int> { 5, 10, 20, 30, 4, 9, 8, 6 };
var result = numbers.Where(x => x > 5);//Here where use Func<TSource in,Bool>
我的问题是为什么LINQ没有使用Predicate< tsource>而不是Func<>?
谢谢
Dinesh Sharma
My question is that why LINQ not used Predicate<tsource> instead of Func<>?
Thanks
Dinesh Sharma
推荐答案
因为它是Func的一个特例,指南说不要使用谓词: LINQ框架设计指南 [ ^ ]
Because it's a special case of a Func, and the guidelines say "don't use predicates": LINQ Framework Design Guidelines[^]
"Do use the new LINQ types "Func<>" and "Expression<>" instead of custom delegates and predicates, when defining new APIs."
并非所有Linq方法都可以使用Predicate,因为它们可以使用多个不同类型的参数。
这里有一个很好的解释: c# - Func vs 。行动与谓词 - 堆栈溢出 [ ^ ]
Not all the Linq methods could use a Predicate since they can use more than one parameter of different types.
There is a good explanation here: c# - Func vs. Action vs. Predicate - Stack Overflow[^]
这篇关于LINQ where子句使用了func<>代表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文