locked
Linq minimal value of integer in object RRS feed

  • Question

  • User-1556678718 posted

    Hi

    I have class Device and it has Price. How do I get the minimal price from all devices? here is the Class

     public class Device
        {
            public int ProductID { get; set; }
            public string ProductTitle { get; set; }
            public int Price{get, set}
        }
    
    

    now in the code I have

    List<Device> Devices = LoadAllDevices(); //loads all devices from database, unnesesary to put the implementation
    
    var minimumPrice=Devices.Select(x=>x.Price!=null && //HERE IS THE PROBLEM TO FIND MINIMUM PRICE OF ALL THE DEVICES);
      

    Please advice how do i find the minimum?

    Friday, November 29, 2019 6:02 AM

Answers

  • User-1780421697 posted

    In LINQ, you can find the minimum element of the given sequence by using Min() function. This method provides the minimum element of the given set of values

    using System;
    using System.Linq;
    using System.Collections.Generic;
    
    public class Simple {
      public static void Main() 
      {
        List<Device> devices =  new List<Device>{ new Device{ Price =20}, new Device{ Price =30}};
    
       var min = devices.Min(d=>d.Price);
       Console.WriteLine(min); 
    
      }
    	
    	public class Device
    	{
    	  public int Price {get;set;}
    	}
    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 29, 2019 6:55 AM
  • User665608656 posted

    Hi RioDD,

    Please advice how do i find the minimum?

    There are several ways to get the minimum of data by LINQ, and you don't have to deal with x=>x.Price!=null, you can directly use the following three methods:

    var min1 = Devices.Min(x => x.Price);
    
    var min2 = Devices.Select(x => x.Price).Min();
    
    var min3 = (from c in Devices
                   select c).Min(c => c.Price);

    Here is the debugging code result : 

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 29, 2019 7:16 AM

All replies

  • User-1780421697 posted

    In LINQ, you can find the minimum element of the given sequence by using Min() function. This method provides the minimum element of the given set of values

    using System;
    using System.Linq;
    using System.Collections.Generic;
    
    public class Simple {
      public static void Main() 
      {
        List<Device> devices =  new List<Device>{ new Device{ Price =20}, new Device{ Price =30}};
    
       var min = devices.Min(d=>d.Price);
       Console.WriteLine(min); 
    
      }
    	
    	public class Device
    	{
    	  public int Price {get;set;}
    	}
    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 29, 2019 6:55 AM
  • User665608656 posted

    Hi RioDD,

    Please advice how do i find the minimum?

    There are several ways to get the minimum of data by LINQ, and you don't have to deal with x=>x.Price!=null, you can directly use the following three methods:

    var min1 = Devices.Min(x => x.Price);
    
    var min2 = Devices.Select(x => x.Price).Min();
    
    var min3 = (from c in Devices
                   select c).Min(c => c.Price);

    Here is the debugging code result : 

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 29, 2019 7:16 AM