Click here to Skip to main content
12,625,698 members (38,522 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#3.0 C#
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());
            }
        }


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);
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
Posted 10-Aug-10 23:59pm
Khaniya4.4K
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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

Google would have helped you faster.
  Permalink  
v2
Comments
Khaniya 11-Aug-10 6:00am
   
Sorry It is not working
I think there is casting issue
It gives error

Could not find an implementation of the query pattern for source type 'System.Collections.ArrayList'. 'Select' not found. Consider explicitly specifying the type of the range variable 'item'.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Comments
Dalek Dave 11-Aug-10 10:32am
   
Good Call
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Use

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

to get the object that have Maximum countPoint.
  Permalink  
Comments
Khaniya 11-Aug-10 5:58am
   
thanks for your reply
but sorry to say you I could not find Max in arrayList

It shows following error

'System.Collections.ArrayList' does not contain a definition for 'Max' and no extension method 'Max' accepting a first argument of type 'System.Collections.ArrayList' could be found (are you missing a using directive or an assembly reference?)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Try this.

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

You've had several suggestions on how to approach your problem. Be a programmer and figure it out. Nobody here is going to sit down and write code to figure it out for you. We simply don't have the time.

The suggestion to use List instead of ArrayList will go a long way to helping you solve your issue.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161205.3 | Last Updated 6 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100