从集合中选择最大的项目,有些标准

从集合中选择最大的项目,有些标准

问题描述:

我是新来的.NET 3.5。 我有一个项目的集合:

i am new to .net 3.5. I have a collection of items:

IList<Model> models;

其中

class Model
{
    public string Name
    {
       get;
       private set;
    }
}

我想获得的元素,它有最长名字的长度。 我试过

I would like to get the element, which has the longest name's length. I tried

string maxItem = models.Max<Model>(model => model.Name.Length);

但它当然返回的最大长度(我需要一个型号)对象。我知道有使用扩展方法这样做的方式,但我不知道怎么办。

but it of course returns the maximum length (and I need a Model object). I know there is a way of doing this using the extension methods but I don't know how.

这是我怎么得到它的工作。也许有更好的方法,我不知道:

This is how I got it to work. Maybe there's a better way, I'm not sure:

    decimal de = d.Max(p => p.Name.Length);
    Model a = d.First(p => p.Name.Length == de);