C# 如何在 List<T> 中形成一个好的谓词委托给 Find() 某些东西?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/242012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I form a good predicate delegate to Find() something in my List<T>?
提问by Pretzel
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)
在查看 MSDN 之后,我仍然不清楚我应该如何使用 T 的成员变量(其中 T 是一个类)形成一个正确的谓词来使用 List 中的 Find() 方法
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?
我的“byYear”谓词应该是什么样的?
(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...)
(MSDN 示例只讨论了恐龙列表,并且只搜索了一个不变的值“saurus”——它没有显示如何将值传递到谓词中......)
EDIT: I'm using VS2005/.NET2.0, so I don't think Lambda notation is available to me...
编辑:我使用的是 VS2005/.NET2.0,所以我不认为 Lambda 表示法对我可用......
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.
EDIT2:在示例中删除了“1999”,因为我可能想根据不同的值以编程方式“查找”。示例更改为使用 for-do 循环从 1980 年到 2000 年的汽车范围。
采纳答案by Matt Hamilton
Ok, in .NET 2.0 you can use delegates, like so:
好的,在 .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);
}
回答by Matt Hamilton
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:
现在您可以将这个函数的结果(它是一个谓词)传递给您的 Find 方法:
my99Car = cars.Find(byYear(1999));
my65Car = cars.Find(byYear(1965));
回答by Dan Finucane
You can use a lambda expression as follows:
您可以按如下方式使用 lambda 表达式:
myCar = carList.Find(car => car.Year == 1999);
回答by Todd
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);
}
回答by Ajaxx
Since you can't use lambda you can just replace it with an anonymous delegate.
由于您不能使用 lambda,因此您可以将其替换为匿名委托。
myCar = carList.Find(delegate(Car car) { return car.Year == i; });
回答by phclummia
You can use this too:
你也可以使用这个:
var existData =
cars.Find(
c => c.Year== 1999);