如何在没有 lambda 的情况下使用 linq 扩展?

发布于 2024-11-09 09:30:18 字数 1823 浏览 0 评论 0 原文

这个例子纯粹是为了学习,不然我就直接用Lambda表达式了。

我想尝试使用没有 lambda 的Where() 扩展方法,只是为了看看它的外观,但我不知道如何让它编译并正常工作。这个例子毫无意义,所以不要费心去试图找出它的任何逻辑。

我基本上只是想知道是否可以在不使用 lambda 的情况下使用扩展方法(仅用于学习目的)以及代码中的样子。

我感到困惑的是,Where() 条件接受 Func,但该方法返回 IEnumerable? Func 的定义方式是,它接受一个 int 并返回一个 bool。如果这是 Func>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Delegates
{
    public class Learning
    {
        /// <summary>
        /// Predicates - specialized verison of Func
        /// </summary>
        public static void Main()
        {
            List<int> list = new List<int> { 1, 2, 3 };

            Func<int, bool> someFunc = greaterThanTwo;
            IEnumerable<int> result = list.Where(someFunc.Invoke(1));

        }


        static IEnumerable<int> greaterThanTwo(int arg, bool isValid)
        {
              return new List<int>() { 1 };
        }

    }
}

更新的代码

public class Learning
    {
        /// <summary>
        /// Predicates - specialized verison of Func
        /// </summary>
        public static void Main()
        {
            // Without lambda
            List<int> list = new List<int> { 1, 2, 3 };

            Func<int, bool> someFunc = greaterThanTwo;
            // predicate of type int
            IEnumerable<int> result = list.Where(someFunc);

        }


        static bool greaterThanTwo(int arg, bool isValid)
        {
            return true;
        }

    }

,我会收到以下错误:

No override for 'greaterThanTwo' matches delegate 'System .Func'

This example is purely for learning, otherwise I would have used Lambda expressions right away.

I would like to try and use the Where() extension method without lambda just to see how it would look, but I can't figure out how to get it to compile and work properly. The example makes no sense so don't bother trying to figure out any logic to it.

I basically just want to know if it is possible to use extensions methods without using lambda (for learning purposes only) and how that would look like in code.

What I'm getting confused with is the Where() condition takes in a Func<int,bool>, but the method returns an IEnumerable<int>? The way the Func is defined, it takes in one int and returns a bool. It would make more sense to me if this was Func<int, bool, IEnumberable<string>>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Delegates
{
    public class Learning
    {
        /// <summary>
        /// Predicates - specialized verison of Func
        /// </summary>
        public static void Main()
        {
            List<int> list = new List<int> { 1, 2, 3 };

            Func<int, bool> someFunc = greaterThanTwo;
            IEnumerable<int> result = list.Where(someFunc.Invoke(1));

        }


        static IEnumerable<int> greaterThanTwo(int arg, bool isValid)
        {
              return new List<int>() { 1 };
        }

    }
}

Updated Code

public class Learning
    {
        /// <summary>
        /// Predicates - specialized verison of Func
        /// </summary>
        public static void Main()
        {
            // Without lambda
            List<int> list = new List<int> { 1, 2, 3 };

            Func<int, bool> someFunc = greaterThanTwo;
            // predicate of type int
            IEnumerable<int> result = list.Where(someFunc);

        }


        static bool greaterThanTwo(int arg, bool isValid)
        {
            return true;
        }

    }

I get the following error:

No overload for 'greaterThanTwo' matches delegate 'System.Func'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

吾家有女初长成 2024-11-16 09:30:18

Where 接受一个函数,该函数接受单个元素(在本例中为 int)作为参数,并接受布尔值作为其返回类型。这称为谓词 - 它给出“是”或“否”答案,可以反复应用于相同类型的元素序列。

您在 greaterThanTwo 函数中出错 - 它需要两个参数,而不是一个 - 并返回一个 IEnumerable - 因此它与 Func。它应该接受一个 int 并返回一个 bool - 同样,这是一个谓词(见上文)。

一旦你解决了这个问题,你的另一个问题是Invoke - 没有调用任何东西 - 你正在将委托(指针)交给一个方法,而内部Where 内部将在需要时调用它。

试试这个:

static bool greaterThanTwo(int arg)
{
    return (arg > 2);
}

//snip

Func<int, bool> someFunc = greaterThanTwo;
IEnumerable<int> result = list.Where(someFunc);

Where takes a function that accepts a single element (in this case, int) as a parameter, and a boolean as its return type. This is called a predicate - it gives a "yes" or "no" answer that can be applied over and over again to a sequence of elements of the same type.

You went wrong at the greaterThanTwo function - it takes two arguments, not one - and returns an IEnumerable<int> - so it's totally incompatible with Func<int, bool>. It should take an int and return a bool - again, this is a predicate (see above).

Once you sort that out, your other problem is Invoke - you aren't invoking anything - you're handing off a delegate (pointer) to a method, and the guts inside of Where will take care of invoking it when it needs.

Try this:

static bool greaterThanTwo(int arg)
{
    return (arg > 2);
}

//snip

Func<int, bool> someFunc = greaterThanTwo;
IEnumerable<int> result = list.Where(someFunc);
萌吟 2024-11-16 09:30:18

定义 Func 定义了一个委托,该委托采用 T 类型的单个参数并返回 TResult 类型的结果。 Linqs Where 子句是针对 IEnumerable 定义的(通过扩展方法间接实现)并采用参数(Where 的基本版本) Func 类型,基本上表示函数必须采用 IEnumerable 类型的参数并返回 bool 值。

为了使您的示例正常工作:

bool greaterThanTwo(int arg)
{
    // this needs to be a boolean operation against the arg
    return arg > 2;
}

// and invoke like
list.Where(greaterThanTwo);

The definition Func<T, TResult> defines a delegate that takes a single parameter of type T and returns a result of type TResult. Linqs Where clause is defined (indirectly via an extension method) against an IEnumerable<T> and takes a parameter (the basic version of Where) of type Func<T, bool> which basically says the function must take a parameter of the type of the IEnumerable and return a bool value.

To make your example work:

bool greaterThanTwo(int arg)
{
    // this needs to be a boolean operation against the arg
    return arg > 2;
}

// and invoke like
list.Where(greaterThanTwo);
要走干脆点 2024-11-16 09:30:18

Func 表示您有一个接受 int 并返回 bool 的函数。如果您想在函数中使用更多参数,可以将它们列出来,但是您定义的最后一个泛型类型是该函数的返回类型。如果您想将其定义为 Func,您的方法将如下所示:Func> 您的函数应如下所示。

namespace Delegates
{
    public class Learning
    {
        /// <summary>
        /// Predicates - specialized verison of Func
        /// </summary>
        public static void Main()
        {
            List<int> list = new List<int> { 1, 2, 3 };

            Func<int, bool> someFunc = greaterThanTwo;
            IEnumerable<int> result = list.Where(someFunc);

        }


        static bool greaterThanTwo(int arg)
        {
              return (arg > 2);
        }

    }
}

Func<int, bool> means that you have a function that takes in an int, and returns a bool. If you want to take more arguments into your function, you can list them out, however the last generic type that you define is the return type of that function. Your method would look something like this if you wanted to define it as a Func, Func<int, bool, IEnumerable<int>> Your function should look something like this.

namespace Delegates
{
    public class Learning
    {
        /// <summary>
        /// Predicates - specialized verison of Func
        /// </summary>
        public static void Main()
        {
            List<int> list = new List<int> { 1, 2, 3 };

            Func<int, bool> someFunc = greaterThanTwo;
            IEnumerable<int> result = list.Where(someFunc);

        }


        static bool greaterThanTwo(int arg)
        {
              return (arg > 2);
        }

    }
}
冷…雨湿花 2024-11-16 09:30:18

Where() 函数对每个项目执行一次 lambda。如果您想返回,例如 List,您将使用 Aggregate() 来代替它提供该功能。但是 Where() 会根据 lambda 的布尔返回值自动添加或不添加项目。

The Where() function executes the lambda once for every item. If you want to return, say a List<int> you would use Aggregate() instead which provides that functionality. But Where() automatically adds or doesn't add the item based on the boolean return value of your lambda.

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