简体   繁体   English

C#LINQ Orderby - true / false如何影响orderby?

[英]C# LINQ Orderby - How does true/false affect orderby?

I was studying a bit of LINQ ordering as I have a list of Ids, and I need to order them sequentially. 我正在研究一些LINQ排序,因为我有一个ID列表,我需要按顺序排序。 However, there are certain ids that need to take precedence over the standard ordering. 但是,某些ID需要优先于标准排序。

Given this C# code (which can be pasted into .NET Fiddle to test) the ordering works as I need it to, but I don't understand why a not ( ! ) operator on a contains is giving me the correct ordering? 鉴于这个C#代码(可以粘贴到.NET Fiddle进行测试),排序按照我的需要工作,但我不明白为什么一个包含的not( ! )运算符给我正确的排序?

My expected ordering output is ( 5, 1, 2, 3, 4, 6, 7, 8, 9 ). 我预期的排序输出是( 5, 1, 2, 3, 4, 6, 7, 8, 9 )。

If I have a Contains in my ordering, shouldn't it give ordering priority to the rows that returned true? 如果我的订单中Contains ,它是否应该为返回true的行赋予优先级? Instead it appears to give ordering priority to rows that return false. 相反,它似乎为返回false的行提供排序优先级。

using System.Linq;
using System;

public class Program
{
  public static void Main()
  {
    var numbersToFilterBy = new [] {5, 11, 20};

    var x = new [] {new XClass(){Id = 1}, new XClass(){Id = 9}, new XClass(){Id = 5}, new XClass(){Id = 3}, new XClass(){Id = 4}, new XClass(){Id = 2}, new XClass(){Id = 6}, new XClass(){Id = 8}, new XClass(){Id = 7}};

    var trueData = (from data in x
                   orderby !numbersToFilterBy.Contains(data.Id), data.Id
                    select data).ToList();

    foreach(var item in trueData){
        Console.WriteLine(item.Id);
  }
}

public class XClass{
    public int Id{get;set;}
  }
}

What is the explanation as to why this happens? 为什么会发生这种情况有什么解释?

The OrderBy method will sort items in ascending order by default . OrderBy方法默认按升序对项目进行排序。 Now, given that the numeric representation of a boolean is: 现在,假设布尔值的数字表示为:

  • false = 0 false = 0
  • true = 1 true = 1

false values will naturally come first. false价值自然会先来。 If you want to reverse the order just use the descending keyword: 如果要反转订单,只需使用descending关键字:

var trueData = (from data in x
               orderby numbersToFilterBy.Contains(data.Id) descending, data.Id
                select data).ToList();

Basically, false is earlier than true ... think of them as false=0, true=1. 基本上, false早于true ...将它们视为false = 0,true = 1。 This is in-keeping with the documentation for bool.CompareTo(bool) . 这与bool.CompareTo(bool)文档保持一致。

If you want to prioritize "true" values to the start, just use OrderByDescending instead. 如果要将“true”值设置为优先级,请使用OrderByDescending

Ordering isn't about priority – it's about ordinal value . 订购不是优先级 - 它是关于序数值 You're doing an ascending order against a boolean value, and false has a lower ordinal value than true in that context. 您正在对布尔值执行升序操作,并且false在该上下文中具有比true更低的序数值。

Let me explain this with an example of a List<bool> . 让我用List<bool>的例子解释一下。 Consider the following snippet: 请考虑以下代码段:

List<bool> BoolList = new List<bool>() { true, false, false, true };
var opList = BoolList.OrderBy(x => x).ToList();

finally the opList will have the values as false , false , true , true which means false come first when we apply OrderBy over a list of boolean values. 最后, opList的值为falsefalsetruetrue ,这意味着当我们将OrderBy应用于布尔值列表时,false将首先出现。 This is because false is consider as 0 and true will be 1 . 这是因为false被认为是0而true将被认为是1

In your case the list is first sorted as 5,1,9,3,4,2,6,8,7 based on the orderby !numbersToFilterBy.Contains(data.Id) then by data.Id will gives you the exact result as 5, 1, 2, 3, 4, 6, 7, 8, 9 . 在您的情况下,列表首先根据orderby !numbersToFilterBy.Contains(data.Id)排序为5,1,9,3,4,2,6,8,7 ,然后按data.Id将为您提供确切的结果为5, 1, 2, 3, 4, 6, 7, 8, 9

If you remove the ! 如果删除了! from the OrderBy it gives you the first sort result like 1,9,3,4,2,6,8,7,5 since the condition is true for 5 hence it will come last in the ordering. OrderBy它给你第一个排序结果,如1,9,3,4,2,6,8,7,5因为条件为5因此它将在排序中排在最后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM