带有Linq的ArrayList,从个类类实例中查找最大值 [英] ArrayList with Linq, Find Maximum from custome class instance

查看:81
本文介绍了带有Linq的ArrayList,从个类类实例中查找最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个arraylist存储我的类的实例名为 PointOnLine



我的班级定义就像是



I am having a arraylist which stored instance of my class named PointOnLine

my class definition is like

public class PointOnLine
        {
            public float degree;
            public LinkedList.Node node;
            public int countPoint;
            public override string ToString()
            {
                return Convert.ToString(degree.ToString() + "        " + countPoint.ToString());
            }
        }







我正在添加大约100到500个实例数组列表中的类如上所述




I am adding approx 100 to 500 instances of above class in array list like this

arrayList.Add(objPointOnLine);
arrayList.Add(objPointOnLine1);
arrayList.Add(objPointOnLine2);

等等



现在我想找到 PointOnLine的实例具有 countPoint 字段的最大值,来自使用Linq或Limbda表达式的arraylist。





谢谢

and so on

Now I want to find instance of PointOnLine having maximum value of countPoint field, from arraylist using Linq or Limbda expression.


Thanks

推荐答案

var max_value = (from item in arrayList
               select item.countPoint).Max();





谷歌会更快地帮助你。



Google would have helped you faster.


为什么人们还在使用任何版本的.NET 2.0或更高版本的 ArrayList ?如果你使用 List< PointOnLine> ,那么之前的两个答案都可能有效。
Why on earth are people still using ArrayList for any version of .NET 2.0 or greater? If you use a List<PointOnLine> instead then both of the previous answers will probably work.


使用



arrayList.Max(a => a.countPoint);



获取具有Maximum countPoint的对象。
Use

arrayList.Max(a => a.countPoint);

to get the object that have Maximum countPoint.


这篇关于带有Linq的ArrayList,从个类类实例中查找最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆