谓词和 OrderBy , Func
我知道谓词是委托给返回 bool 并采用泛型参数的函数,我知道当我说:
mycustomer => mycustomer.fullname == 1
它实际上意味着:
delegate (Customer mycustomer)
{
return mycustomer.fullName == "John";
}
当我传递此 lambda 表达式时传入的参数是:
public delegate bool Criteriapublic delegate bool Criteria
public delegate bool Criteria
T>(T value)
本身称为 Predicate
但我不明白的是当我
在 customers.OrderBy 中 说
mycustomer=>mycustomer.fullname
时它意味着什么(mycustomer=>mycustomer.fullname);
如何实现类似 OrderBy
的功能?我如何告诉方法对哪个属性执行操作!就像前面的例子一样?
通过这里的例子,我想创建一个方法来获取特定属性的集合的所有值:
list<string> mylist = customers.GetPropertyValues(cus=>cus.Fullname);
提前致谢。
I understand that predicates are delegate to function which return bool and take generic parameter, I understand that when I say:
mycustomer => mycustomer.fullname == 1
It actually means:
delegate (Customer mycustomer)
{
return mycustomer.fullName == "John";
}
The paramter I'm passing in when I pass this lambda expression is:
public delegate bool Criteria<T>(T value)
which is natively called Predicate
But what I don't understand is what it means when I say mycustomer=>mycustomer.fullname
In customers.OrderBy(mycustomer=>mycustomer.fullname);
How do I implement something like OrderBy
? How do I tell a method which property to do action on ! like the previous example?
By example here is a case I want to make a method which get all values of a collection for a specific property :
list<string> mylist = customers.GetPropertyValues(cus=>cus.Fullname);
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Func
用于创建IComparer
,它在OrderedEnumerable
内部使用来对项目进行排序。当您这样做时:OrderedEnumerable
类型正在内部创建一个IComparer
。在上面的示例中,如果i.SomeProperty
是String
,它将创建IComparer
的实例,然后对在SomeProperty
成员上使用该比较器进行源枚举。在最后一种情况下:
您可以使用
Select
执行此操作:这将返回可枚举的
String
名称。在Select
方法中,Func
用于选择要添加到结果中的目标元素。要自己复制这个,你可以这样做:
The
Func<TElement,TKey>
is used to create anIComparer<TKey>
which is used internally in anOrderedEnumerable
to sort the items. When you do:The
OrderedEnumerable
type is creating anIComparer<TKey>
internally. In the above example, ifi.SomeProperty
were aString
it would create an instance ofIComparer<String>
and then sort the items in the source enumerable using that comprarer on theSomeProperty
member.In your last case:
You do this using
Select
:Which will return an enumerable of
String
names. In theSelect
method, theFunc<TSource, TResult>
is used to select the target element to be added to the result.To replicate this yourself, you could do:
它的含义与您的委托示例相同,只是它只返回 fullName。作为 lambda 传递,以便可以延迟计算。
如果您想创建一个类似于
OrderBy
的方法,除此之外,您的 GetPropertyValues 似乎与customers.Select(c => c.Fullname)
相同,那么您必须深入研究泛型和扩展方法的海洋。OrderBy
实际上只是IEnumerable
的一个扩展方法,定义为看起来 @Mike 的答案就是一个例子,也是 LINQ
Select
的重新实现代码>.It's the same thing as your delegate example except it only returns the fullName. Passed as a lambda so that it can be lazy evaluated.
If you want to create a method that works like
OrderBy
, besides that your GetPropertyValues seems to be the same ascustomers.Select(c => c.Fullname)
, then you'll have to dive into the sea of generics and extension methods.OrderBy
is actually just an extension method onIEnumerable
defined asIt looks like the answer from @Mike is an example of that and also a reimplementation of LINQ
Select
.你可以创建这样的东西:
但我真的不明白为什么 Select 方法不适合你。
You can create something like this:
But I really don't understand why Select method doesn't suite you.
Enumerable
类上的OrderBy 扩展方法
采用Func
类型的方法作为参数。 这里是文档。这里是另一篇您可以使用的好文章。
The
OrderBy Extension Method
on classEnumerable
takes a method of typeFunc
as argument. Here is the documentation.Here is another good article that you can use.