如何为 List中的 Find() 内容形成良好的谓词委托?

发布于 2024-07-08 04:01:53 字数 794 浏览 11 评论 0原文

查看 MSDN 后,我仍然不清楚如何形成一个正确的谓词来使用 T 的成员变量(其中 T 是一个类)在 List 中使用 Find() 方法,

例如:

public class Car
{
   public string Make;
   public string Model;
   public int Year;
}

{  // somewhere in my code
   List<Car> carList = new List<Car>();
   // ... code to add Cars ...

   Car myCar = new Car();

   // Find the first of each car made between 1980 and 2000
   for (int x = 1980; x < 2000; x++)
   {
       myCar = carList.Find(byYear(x));
       Console.Writeline(myCar.Make + myCar.Model);
   }
}

我的“byYear”谓词应该是什么样子喜欢?

(MSDN 示例仅讨论恐龙列表,并且仅搜索不变的值“saurus”——它没有显示如何将值传递到谓词中...)

编辑:我正在使用 VS2005/.NET2 .0,所以我认为 Lambda 表示法对我来说不可用...

编辑2:删除了示例中的“1999”,因为我可能想根据不同的值以编程方式“查找”。 使用 for-do 循环将示例更改为从 1980 年到 2000 年的汽车系列。

After looking on MSDN, it's still unclear to me how I should form a proper predicate to use the Find() method in List using a member variable of T (where T is a class)

For example:

public class Car
{
   public string Make;
   public string Model;
   public int Year;
}

{  // somewhere in my code
   List<Car> carList = new List<Car>();
   // ... code to add Cars ...

   Car myCar = new Car();

   // Find the first of each car made between 1980 and 2000
   for (int x = 1980; x < 2000; x++)
   {
       myCar = carList.Find(byYear(x));
       Console.Writeline(myCar.Make + myCar.Model);
   }
}

What should my "byYear" predicate look like?

(The MSDN example only talks about a List of dinosaurs and only searches for an unchanging value "saurus" -- It doesn't show how to pass a value into the predicate...)

EDIT: I'm using VS2005/.NET2.0, so I don't think Lambda notation is available to me...

EDIT2: Removed "1999" in the example because I may want to "Find" programatically based on different values. Example changed to range of cars from 1980 to 2000 using for-do loop.

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

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

发布评论

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

评论(6

寄居者 2024-07-15 04:01:53

您可以使用 lambda 表达式,如下所示:

myCar = carList.Find(car => car.Year == 1999);

You can use a lambda expression as follows:

myCar = carList.Find(car => car.Year == 1999);
迎风吟唱 2024-07-15 04:01:53

好的,在 .NET 2.0 中您可以使用委托,如下所示:

static Predicate<Car> ByYear(int year)
{
    return delegate(Car car)
    {
        return car.Year == year;
    };
}

static void Main(string[] args)
{
    // yeah, this bit is C# 3.0, but ignore it - it's just setting up the list.
    List<Car> list = new List<Car>
    {
        new Car { Year = 1940 },
        new Car { Year = 1965 },
        new Car { Year = 1973 },
        new Car { Year = 1999 }
    };
    var car99 = list.Find(ByYear(1999));
    var car65 = list.Find(ByYear(1965));

    Console.WriteLine(car99.Year);
    Console.WriteLine(car65.Year);
}

Ok, in .NET 2.0 you can use delegates, like so:

static Predicate<Car> ByYear(int year)
{
    return delegate(Car car)
    {
        return car.Year == year;
    };
}

static void Main(string[] args)
{
    // yeah, this bit is C# 3.0, but ignore it - it's just setting up the list.
    List<Car> list = new List<Car>
    {
        new Car { Year = 1940 },
        new Car { Year = 1965 },
        new Car { Year = 1973 },
        new Car { Year = 1999 }
    };
    var car99 = list.Find(ByYear(1999));
    var car65 = list.Find(ByYear(1965));

    Console.WriteLine(car99.Year);
    Console.WriteLine(car65.Year);
}
煞人兵器 2024-07-15 04:01:53

或者您可以使用匿名委托:

Car myCar = cars.Find(delegate(Car c) { return c.Year == x; });

// If not found myCar will be null
if (myCar != null)
{
     Console.Writeline(myCar.Make + myCar.Model);
}

Or you can use an anonymous delegate:

Car myCar = cars.Find(delegate(Car c) { return c.Year == x; });

// If not found myCar will be null
if (myCar != null)
{
     Console.Writeline(myCar.Make + myCar.Model);
}
小巷里的女流氓 2024-07-15 04:01:53

由于您无法使用 lambda,因此您可以将其替换为匿名委托。

myCar = carList.Find(delegate(Car car) { return car.Year == i; });

Since you can't use lambda you can just replace it with an anonymous delegate.

myCar = carList.Find(delegate(Car car) { return car.Year == i; });
黒涩兲箜 2024-07-15 04:01:53

唔。 仔细考虑一下,您可以使用柯里化来返回谓词。

Func<int, Predicate<Car>> byYear = i => (c => c.Year == i);

现在您可以将此函数的结果(这是一个谓词)传递给您的 Find 方法:

my99Car = cars.Find(byYear(1999));
my65Car = cars.Find(byYear(1965));

Hmm. Thinking more about it, you could use currying to return a predicate.

Func<int, Predicate<Car>> byYear = i => (c => c.Year == i);

Now you can pass the result of this function (which is a predicate) to your Find method:

my99Car = cars.Find(byYear(1999));
my65Car = cars.Find(byYear(1965));
旧话新听 2024-07-15 04:01:53

你也可以使用这个:

var existData =
    cars.Find(
     c => c.Year== 1999);

You can use this too:

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