如何使用LINQ来删除List中的Min和Max值

我有一个如下所示的列表。

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

我将使用LINQ删除Above List中的Min和Max值。

例如,下面的代码段只是示例,不起作用。

 var newValue = from pair in temp select pair  temp.Min() 

希望,我希望结果如下;

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

我试过谷歌搜索,但还没找到合适的例子。

当我使用LINQ时它是否可以工作? 谢谢你的时间。

你应该在where使用。

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

您当前的方法将选择值是否在范围内,而不是过滤它们。 这就是where子句的用途。

试试这个:-

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

工作小提琴 。

如果你只需要删除最小值和最大值,为什么不只是使用remove()? 这对需要什么?

  List temp =new List() { 3, 5, 6, 8, 2, 1, 6 }; temp.Remove(temp.Max()); temp.Remove(temp.Min()); 

或类似的东西,如果你需要保持温度,宁愿在副本上工作

 temp.Sort(); temp.Skip(1).Take(temp.Count - 2).ToList(); 

你怎么能在Generic Collection中添加一个数组。 您还必须将查询结果转换为列表。 使用@Matthew Haugen建议的where子句。

 List temp = new List();// {3, 5, 6, 8, 2, 1, 6} temp.Add(3); temp.Add(5); temp.Add(6); temp.Add(8); temp.Add(2); temp.Add(1); temp.Add(6); List newValue = (from n in temp where n > temp.Min() & n < temp.Max() Select n).ToList();