Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0
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 22:59pm
Khaniya4.1K
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 at 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 at 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 at 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
0 OriginalGriff 385
1 Sergey Alexandrovich Kryukov 195
2 Prakriti Goyal 177
3 jlopez788 134
4 Kruti Joshi 117
0 OriginalGriff 6,742
1 Sergey Alexandrovich Kryukov 5,479
2 Maciej Los 3,474
3 Peter Leow 3,313
4 DamithSL 2,505


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 6 Feb 2013
Copyright © CodeProject, 1999-2014
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