Saturday, August 20, 2022

C# Linq Min method

// Online C# Editor for free

// Write, Edit and Run your C# code using C# Online Compiler


using System;

using System.Linq;

using System.Collections.Generic;


public class HelloWorld

{

    public static void Main(string[] args)

    {

        List<test> testlist = new List<test>();

        testlist.Add(new test { id = 5});

        testlist.Add(new test { id = 7});

        testlist.Add(new test { id = 2});

        testlist.Add(new test { id = 8});

        //find the min

        var min = testlist.Min(x=>x.id);

        

        //get the object with min component

        var y = testlist.Where(x=>x.id == min).FirstOrDefault();

        Console.WriteLine ($"The min is  {min} {y}  {y.id}");

    }

    

    public class test { 

        public int id { get;set;} 

    } 

}

Output: 

The min is  2 HelloWorld+test  2


NOTE: {y} prints "HelloWorld+test" which is the object hierarchy of parent class and contained class.


No comments:

Post a Comment

How to check local and global angular versions

 Use the command ng version (or ng v ) to find the version of Angular CLI in the current folder. Run it outside of the Angular project, to f...