按多个属性和条件对对象列表进行排序和分组

时间:2019-10-30 14:50:37

标签: c# linq sql-order-by

我有一个具有以下属性的对象列表:

日期(常规日期格式)
高优先级(boolen)
优先级(整数)
已完成(布尔值)
关闭(布尔值)

我正在尝试使用linq将列表排序为以下逻辑:

如果未完成,则按高优先级排序
如果没有完成,则优先处理
然后没有优先级
然后优先完成(
然后优先完成(
) 然后关闭

我还需要按日期对每个分组进行排序。

这是我当前的linq查询:

Tasks.GroupBy(x => x.Date)
    .Select(s => 
        s.OrderByDescending(a => a.HighPriority)
         .ThenBy(a => a.Priority)
         .ThenBy(a => a.Closed))
    .SelectMany(sa => sa)
    .ToList();

如何更改查询以获取符合我的逻辑的列表?

1 个答案:

答案 0 :(得分:1)

import ast import hashlib import os import base64 from Crypto.Cipher import AES IV_SIZE = 16 # 128 bit, fixed for the AES algorithm KEY_SIZE = 32 # 256 bit meaning AES-256, can also be 128 or 192 bits SALT_SIZE = 16 # This size is arbitrary cleartext = b'Lorem ipsum' password = input('Password: ') password = bytes(password, 'utf-8') salt = os.urandom(SALT_SIZE) derived = hashlib.pbkdf2_hmac('sha256', password, salt, 100000, dklen=IV_SIZE + KEY_SIZE) iv = derived[0:IV_SIZE] key = derived[IV_SIZE:] encrypted = salt + AES.new(key, AES.MODE_CFB, iv).encrypt(cleartext) print(encrypted) ############################################ encrypted = ast.literal_eval(str(encrypted)) ########################################### encryptedString = base64.encodebytes(encrypted) print(encryptedString) encrypted = base64.decodebytes(encryptedString) # <- get the bytes back salt = encrypted[0:SALT_SIZE] derived = hashlib.pbkdf2_hmac('sha256', password, salt, 100000, dklen=IV_SIZE + KEY_SIZE) iv = derived[0:IV_SIZE] key = derived[IV_SIZE:] cleartext = AES.new(key, AES.MODE_CFB, iv).decrypt(encrypted[SALT_SIZE:]) print(cleartext) 是一个类的实例的集合。在该类中创建一个名为Tasks的计算属性。

OrderPriority

然后您的LINQ看起来像:

public byte OrderPriority
{
    get
    {
        if (HighPriority && !Completed)
            return 1;
        if (Priority && !Completed)
            return 2;
        // and so forth
    }
}