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

编程入门 行业动态 更新时间:2024-10-27 08:24:44
本文介绍了从集合中选择最大的项目,有些标准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

i am new to 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);

更多推荐

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

本文发布于:2023-10-16 20:26:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1498646.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标准   项目

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!