Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. The Max() linq extension method on a List<MyClass> [Solved]

The Max() linq extension method on a List<MyClass> [Solved]

Scheduled Pinned Locked Moved C#
csharplinqquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TMattC
    wrote on last edited by
    #1

    Im not very good at LINQ (yet), but I understand that I by using the LINQ extension methods in the Enumerable class can do like this to get the largest value in the sequence:

    List lst = new List() { 1,2,3,4,5,6,7,8 };
    int max = lst.Max();

    But what if my List looks like this?

    public class MyClass
    {
    public int Id { get; set; }
    public MyClass(int _id)
    { Id = _id; }
    }

    List lst = new List() { new MyClass(1), new MyClass(2), new MyClass(3) };

    I want to get the Id value from the instance with the highest Id value. Im under the impression that I should write a MyClass method and pass it to the Max(...) extension method, but I cant seem to figure out how it would be done. Anyone?

    T Richard DeemingR 2 Replies Last reply
    0
    • T TMattC

      Im not very good at LINQ (yet), but I understand that I by using the LINQ extension methods in the Enumerable class can do like this to get the largest value in the sequence:

      List lst = new List() { 1,2,3,4,5,6,7,8 };
      int max = lst.Max();

      But what if my List looks like this?

      public class MyClass
      {
      public int Id { get; set; }
      public MyClass(int _id)
      { Id = _id; }
      }

      List lst = new List() { new MyClass(1), new MyClass(2), new MyClass(3) };

      I want to get the Id value from the instance with the highest Id value. Im under the impression that I should write a MyClass method and pass it to the Max(...) extension method, but I cant seem to figure out how it would be done. Anyone?

      T Offline
      T Offline
      TMattC
      wrote on last edited by
      #2

      I solved it by myself, turned out it wasnt so complicated.

      int m = lst.Max(mc => mc.Id);

      1 Reply Last reply
      0
      • T TMattC

        Im not very good at LINQ (yet), but I understand that I by using the LINQ extension methods in the Enumerable class can do like this to get the largest value in the sequence:

        List lst = new List() { 1,2,3,4,5,6,7,8 };
        int max = lst.Max();

        But what if my List looks like this?

        public class MyClass
        {
        public int Id { get; set; }
        public MyClass(int _id)
        { Id = _id; }
        }

        List lst = new List() { new MyClass(1), new MyClass(2), new MyClass(3) };

        I want to get the Id value from the instance with the highest Id value. Im under the impression that I should write a MyClass method and pass it to the Max(...) extension method, but I cant seem to figure out how it would be done. Anyone?

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        If you only want the value of the maximum ID, you can use either:

        int maxId = lst.Select(x => x.Id).Max();

        or:

        int maxId = lst.Max(x => x.Id);

        If you want to get the instance of MyClass which has the maximum ID, you'll need to implement the IComparable<T> interface[^]:

        public class MyClass : IComparable<MyClass>
        {
        public int Id { get; set; }

        public MyClass(int \_id)
        { 
            Id = \_id; 
        }
        
        public int CompareTo(MyClass other)
        {
            if (other == null) return 1;
            return Id.CompareTo(other.Id);
        }
        

        }
        ...
        MyClass maxId = lst.Max();

        Unfortunately, there isn't an overload which takes an IComparer<T> instance[^], so if you need different sort orders in different situations, you're stuck with writing your own Max method.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups