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.


 using Microsoft.AspNetCore.Mvc; using System.Xml.Linq; using System.Xml.XPath; //<table class="common-table medium js-table js-stre...