Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a LINQ in order to remove Min and Max value in List

Tags:

c#

list

linq

I have a List such as below.

List<int> temp = new List<int> { 3, 5, 6, 8, 2, 1, 6};

I'm going to use a LINQ to remove Min and Max value in Above List.

For example, below snippet code is just example, not working.

var newValue = from pair in temp
               select pair < temp.Max() && pair > temp.Min()

Hopefully, I expect the result like below ;

newValue = {3, 5, 6, 2, 6 }

I've tried Googling, but couldn't find proper example yet.

Is it workigng when I use a LINQ ? Thanks for your time.

like image 457
Changju.rhee Avatar asked Dec 25 '22 02:12

Changju.rhee


2 Answers

Try this:-

var query = temp.Where(x => x != temp.Min() && x != temp.Max()).ToList();

Working Fiddle.

like image 23
Rahul Singh Avatar answered Dec 27 '22 18:12

Rahul Singh


You should be using where.

from pair in temp
where pair < temp.Max() && pair > temp.Min()
select pair

Your current approach will select whether the values are in range, not filter them. That's what the where clause is for.

like image 80
Matthew Haugen Avatar answered Dec 27 '22 17:12

Matthew Haugen