谓词和 OrderBy , Func

发布于 2024-09-12 18:21:51 字数 810 浏览 3 评论 0原文

我知道谓词是委托给返回 bool 并采用泛型参数的函数,我知道当我说:

mycustomer => mycustomer.fullname == 1

它实际上意味着:

delegate (Customer mycustomer)
{
  return mycustomer.fullName == "John";
}

当我传递此 lambda 表达式时传入的参数是:

public delegate bool Criteriapublic delegate bool Criteriapublic delegate bool CriteriaT>(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

仄言 2024-09-19 18:21:51

Func 用于创建 IComparer,它在 OrderedEnumerable 内部使用来对项目进行排序。当您这样做时:

var items = myList.OrderBy(i => i.SomeProperty);

OrderedEnumerable 类型正在内部创建一个 IComparer。在上面的示例中,如果 i.SomePropertyString,它将创建 IComparer 的实例,然后对在 SomeProperty 成员上使用该比较器进行源枚举。

在最后一种情况下:

list<string> mylist = customers.GetPropertyValues(cus=>cus.Fullname);

您可以使用 Select 执行此操作:

var names = customers.Select(c => c.Fullname);

这将返回可枚举的 String 名称。在Select方法中,Func用于选择要添加到结果中的目标元素。

要自己复制这个,你可以这样做:

public static IEnumerable<TMember> GetPropertyValues<TSource, TMember>
  (this IEnumerable<TSource> enumerable, Func<TSource, TMember> selector)
{
  if (enumerable == null)
    throw new ArgumentNullException("enumerable");

  foreach (TSource item in enumerable)
  {
    yield return selector(item);
  }
}

The Func<TElement,TKey> is used to create an IComparer<TKey> which is used internally in an OrderedEnumerable to sort the items. When you do:

var items = myList.OrderBy(i => i.SomeProperty);

The OrderedEnumerable type is creating an IComparer<TKey> internally. In the above example, if i.SomeProperty were a String it would create an instance of IComparer<String> and then sort the items in the source enumerable using that comprarer on the SomeProperty member.

In your last case:

list<string> mylist = customers.GetPropertyValues(cus=>cus.Fullname);

You do this using Select:

var names = customers.Select(c => c.Fullname);

Which will return an enumerable of String names. In the Select method, the Func<TSource, TResult> is used to select the target element to be added to the result.

To replicate this yourself, you could do:

public static IEnumerable<TMember> GetPropertyValues<TSource, TMember>
  (this IEnumerable<TSource> enumerable, Func<TSource, TMember> selector)
{
  if (enumerable == null)
    throw new ArgumentNullException("enumerable");

  foreach (TSource item in enumerable)
  {
    yield return selector(item);
  }
}
ヅ她的身影、若隐若现 2024-09-19 18:21:51

但我不明白当我说 mycustomer=>mycustomer.fullname 时这意味着什么

它​​的含义与您的委托示例相同,只是它只返回 fullName。作为 lambda 传递,以便可以延迟计算。

如果您想创建一个类似于 OrderBy 的方法,除此之外,您的 GetPropertyValues 似乎与 customers.Select(c => c.Fullname) 相同,那么您必须深入研究泛型和扩展方法的海洋。 OrderBy 实际上只是 IEnumerable 的一个扩展方法,定义为

public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
    this IEnumerable<TSource> source,
    Func<TSource, TKey> keySelector
)

看起来 @Mike 的答案就是一个例子,也是 LINQ Select 的重新实现代码>.

but what i dont understand is what it means when i say mycustomer=>mycustomer.fullname

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 as customers.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 on IEnumerable defined as

public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
    this IEnumerable<TSource> source,
    Func<TSource, TKey> keySelector
)

It looks like the answer from @Mike is an example of that and also a reimplementation of LINQ Select.

清秋悲枫 2024-09-19 18:21:51

你可以创建这样的东西:

static class ExtensionClass
{
    static IEnumerable<V> GetPropertyValues<T, V>(this IList<T> collection, Func<T, V> func)
    {
        foreach(var item in collection)
        {
            yield retun func(item);
        }
    }
}

但我真的不明白为什么 Select 方法不适合你。

You can create something like this:

static class ExtensionClass
{
    static IEnumerable<V> GetPropertyValues<T, V>(this IList<T> collection, Func<T, V> func)
    {
        foreach(var item in collection)
        {
            yield retun func(item);
        }
    }
}

But I really don't understand why Select method doesn't suite you.

落花浅忆 2024-09-19 18:21:51

Enumerable 类上的 OrderBy 扩展方法 采用 Func 类型的方法作为参数。 这里是文档。

这里是另一篇您可以使用的好文章。

The OrderBy Extension Method on class Enumerable takes a method of type Func as argument. Here is the documentation.

Here is another good article that you can use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文