How To select max id and min id from list of objects in LINQ or Lambda Expression [Resolved]

Posted by Kasani007 under LINQ on 6/8/2016 | Points: 10 | Views : 453 | Status : [Member] | Replies : 4
How To select max id and min id from list of objects in LINQ or Lambda Expression




Responses

Posted by: Rajnilari2015 on: 6/9/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
@Kasani007 Sir, you can simply use the MAX and MIN function of the collection e.g.

var intList = new List<int>() { 1, 2, 3, 4, 4, 5, 67, 7, 8 };

// By using the max, min function of the generic collection
var max_minIDs = new { MaxID = intList.Max(), MinID = intList.Min() };


Result
-----------
MaxID=67, MinID=1

--
Thanks & Regards,
RNA Team

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sheonarayan on: 6/8/2016 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
Here are the code you need.

To get the max AutoId (property name)
var max = query1.Max(s => s.AutoId);

To get the min AutoId (property name)
var min = query1.Min(s => s.AutoId);

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A2H on: 6/8/2016 [Member] [MVP] Silver | Points: 25

Up
0
Down
An alternative option is to order the list first and then get the first value as min and last value as max.
Sample Code
//Order the list
var orderedAddresses = addr1.OrderBy(x => x.Id);
//Get the first value which is min
var minvalue = orderedAddresses.First();
//Get the last value which is max
var maxvalue = orderedAddresses.Last();

PS: I still recomment Sheo sir's approach and this is for your reference

Complete code
List<Addresses> addr1 = new List<Addresses>();
//Loading dummy values
Addresses objAddress = new Addresses();
objAddress.Id = 1;
objAddress.Address = "Test Address1";
addr1.Add(objAddress);

Addresses objAddress2 = new Addresses();
objAddress2.Id = 2;
objAddress2.Address = "Test Address2";
addr1.Add(objAddress2);

Addresses objAddress3 = new Addresses();
objAddress3.Id = 3;
objAddress3.Address = "Test Address3";
addr1.Add(objAddress3);

Addresses objAddress4 = new Addresses();
objAddress4.Id = 4;
objAddress4.Address = "Test Address4";
addr1.Add(objAddress4);

Addresses objAddress7 = new Addresses();
objAddress7.Id = 7;
objAddress7.Address = "Test Address7";
addr1.Add(objAddress7);

//Order the list
var orderedAddresses = addr1.OrderBy(x => x.Id);
//Get the first value which is min
var minvalue = orderedAddresses.First();
//Get the last value which is max
var maxvalue = orderedAddresses.Last();


Class:
  public class Addresses
{
public long? Id { get; set; }
public string Address { get; set; }
}


Thanks,
A2H
My Blog

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Muneshs16 on: 6/11/2016 [Member] Starter | Points: 25

Up
0
Down
var maxId = query1.Max(s => s.AutoId).SingleOrDefault();


var minId = query1.Min(s => s.AutoId).SingleOrDefault();


Munesh

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response