[RESOLVED] C# - LINQ Lowest Two Values
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: [RESOLVED] C# - LINQ Lowest Two Values

  1. #1
    Join Date
    Jul 2005
    Location
    Louisville, KY
    Posts
    201

    [RESOLVED] C# - LINQ Lowest Two Values

    I'm currently using a List<> with a class that contains a value Weight. As an example:

    Code:
    public class Nodes
    {
            public Nodes()
            {
                 // some stuff
            }
    
            private double m_weight;
            public double Weight
            {
                get { return this.m_distance; }
                set { m_distance = value; }
            }     
    }
    In my Main class, I'm simply doing a

    Code:
            List<Nodes> Population = new List<Nodes>();
            
            // some stuff
            Nodes LowestWeight = new Nodes();
            Nodes SecondLowestWeight = new Nodes();
            Nodes HighestWeight = new Nodes();
            
            // some more stuff
    At this point how would I use LINQ to get the lowest, second lowest, and highest "Nodes" in Population? I read about doing something, such as

    Code:
    LowestWeight = Population.Min();
    HighestWeight = Population.Max();
    but how can I get the Second Lowest? or is that syntax even correct? Any help is appreciated.

  2. #2
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: C# - LINQ Lowest Two Values

    I think you could use the OrderBy method
    It's not a bug, it's a feature!

  3. #3
    Join Date
    May 2007
    Posts
    1,546

    Re: C# - LINQ Lowest Two Values

    Convert to a List<T> and call sort. It's probably the most efficient way.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  4. #4
    Join Date
    Jan 2010
    Posts
    1,102

    Re: C# - LINQ Lowest Two Values

    A more complicated LINQ query would be more suited to select a specific subset of data based on some predicate, but for this, it's better to keep it simple, as suggested above.

    Alternatively you could do something like this:
    Code:
    Nodes max = Population.Max();
    Nodes min = Population.Min();
    
    Population.Remove(min);
    
    Nodes nextToMin = Population.Min();
    This way you can avoid sorting, and if you need to perserve the original structure of the list, you can put the min back in. Wow, it rimes!

    Aside from that, it's a good idea to try and make your code comply with widespread C# naming conventions - local variables such as Population should use camelCasing, so I would rename that to population instead.
    These conventions help both you and us (or any potential team members in a team project) understand the code more quickly. Besides, if you give this application a rest, and then come back some time, you'll be quicker to pick up where you left of if your naming conventions are consistent throughout your work.

  5. #5
    Join Date
    Jul 2005
    Location
    Louisville, KY
    Posts
    201

    Re: C# - LINQ Lowest Two Values

    I normally use camelCase, I often just write sample code segments, in my forum posts. However, Thanks for the help with that quick snippet, didn't even think about that. LINQ is seeming to be quite helpful. Just started learning it this week.
    Last edited by QuinnJohns; October 10th, 2011 at 11:58 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center