I asked below question. I got an answer.
How to use a LINQ in order to remove Min and Max value in List
However, I have a problem in such scenario there are several min and max values in List. I want to use a LINQ expression in order to remove only one min and Max value in List.
Code snippet :
namespace ConsoleApplication_lamdaTest
{
public struct TValue
{
public double x, y;
public double value { get { return Math.Sqrt(x * x + y * y); } }
}
class Program
{
static void Main(string[] args)
{
List<TValue> temp = new List<TValue> {
new TValue { x = 1, y =2 },
new TValue { x = 3, y =4 },
new TValue { x = 4, y =3 },
new TValue { x = 3, y =1 },
new TValue { x = 2, y =3 },
new TValue { x = 1, y =4 },
new TValue { x = 1, y =2 },
new TValue { x = 1, y =2 }
};
foreach(TValue item in temp)
Console.WriteLine(item.value.ToString());
var newValue = from pair in temp
where pair.value < temp.Max(m => m.value) && pair.value > temp.Min(m => m.value)
select pair;
foreach (TValue item in newValue)
Console.WriteLine(item.value.ToString());
Console.ReadKey();
}
}
}
output is ;
2.23606797749979
5
5
3.16227766016838
3.60555127546399
4.12310562561766
2.23606797749979
2.23606797749979
-------------------
3.16227766016838
3.60555127546399
4.12310562561766
but, I want to get output like below ;
2.23606797749979
5
5
3.16227766016838
3.60555127546399
4.12310562561766
2.23606797749979
2.23606797749979
-------------------
5
3.16227766016838
3.60555127546399
4.12310562561766
2.23606797749979
2.23606797749979
I'm thinking about several steps to solve this issue. Using LINQ: Is it Possible to use LINQ ?
1. Sorting
2. Remove First and Last index
Code Snippet : Any Help ?
var newSortedValue = from pair in temp
orderby pair.value descending
where pair = temp.RemoveAt(0) && pair = temp.RemoveAt(temp.Count()-1)
select pair;
orderedList.Skip(1).Take(orderedList.Count() - 2)should do the trick.