c#linq按null排序

时间:2018-07-26 14:23:26

标签: c# linq sorting

我有一个日志列表作为字符串。它是这样输出给我的:

"2018.07.26 10:35:06:7889: 4d50b064-d269-4256-a187-82a3f9402735 - Client successfully got the transaction result",
"2018.07.26 10:35:06:7959: 4d50b064-d269-4256-a187-82a3f9402735 - Run Transaction Requested (SOCS)... Success",
"2018.07.26 10:35:06:9019: DAIW - New Session Requested... Success (Start Session Thread Started)",
"2018.07.26 10:35:06:9169: 4d50b064-d269-4256-a187-82a3f9402735 - Run Transaction Thread (SOCS)... Success",
"2018.07.26 10:35:06:9229: 4d50b064-d269-4256-a187-82a3f9402735 - Client successfully got the transaction result",
"2018.07.26 10:35:07:1219: DAIW - Start Session ThreadSuccess (c0c2311a-b509-4e6e-a236-80e2d86f2647)",
"2018.07.26 10:35:07:1229: c0c2311a-b509-4e6e-a236-80e2d86f2647 - Client Successfully Retrieved Session",
"2018.07.26 10:35:07:1429: 4d50b064-d269-4256-a187-82a3f9402735 - End Session Requested - Thread started"

这是错误的顺序。 应该按以下顺序:

"2018.07.26 10:35:06:7889: 4d50b064-d269-4256-a187-82a3f9402735 - Client successfully got the transaction result",
"2018.07.26 10:35:06:7959: 4d50b064-d269-4256-a187-82a3f9402735 - Run Transaction Requested (SOCS)... Success",
"2018.07.26 10:35:06:9169: 4d50b064-d269-4256-a187-82a3f9402735 - Run Transaction Thread (SOCS)... Success",
"2018.07.26 10:35:06:9229: 4d50b064-d269-4256-a187-82a3f9402735 - Client successfully got the transaction result",,
"2018.07.26 10:35:07:1429: 4d50b064-d269-4256-a187-82a3f9402735 - End Session Requested - Thread started"
"2018.07.26 10:35:06:9019: DAIW - New Session Requested... Success (Start Session Thread Started)",
"2018.07.26 10:35:07:1219: DAIW - Start Session ThreadSuccess (c0c2311a-b509-4e6e-a236-80e2d86f2647)",
"2018.07.26 10:35:07:1229: c0c2311a-b509-4e6e-a236-80e2d86f2647 - Client Successfully Retrieved Session"

看来旧系统只是按日期排序。 我已经创建了一个模型并提取了所需的零件。 该模型如下所示:

public class TroposLog
{
    public DateTime Created { get; set; }
    public string UserName { get; set; }
    public string SessionId { get; set; }
    public string ActionName { get; set; }
    public string Message { get; set; }
}

现在,这给了我一些帮助。 但是我无法按照我想要的方式订购它。 如果我这样做:

return troposLogs
           .OrderBy(m => m.UserName)
           .ThenBy(m => m.SessionId)
           .ThenBy(m => m.Created);

它返回以下顺序:

"2018.07.26 10:35:06:7889: 4d50b064-d269-4256-a187-82a3f9402735 - Client successfully got the transaction result",
"2018.07.26 10:35:06:7959: 4d50b064-d269-4256-a187-82a3f9402735 - Run Transaction Requested (SOCS)... Success",
"2018.07.26 10:35:06:9169: 4d50b064-d269-4256-a187-82a3f9402735 - Run Transaction Thread (SOCS)... Success",
"2018.07.26 10:35:06:9229: 4d50b064-d269-4256-a187-82a3f9402735 - Client successfully got the transaction result",
"2018.07.26 10:35:07:1429: 4d50b064-d269-4256-a187-82a3f9402735 - End Session Requested - Thread started",
"2018.07.26 10:35:07:1229: c0c2311a-b509-4e6e-a236-80e2d86f2647 - Client Successfully Retrieved Session",
"2018.07.26 10:35:06:9019: DAIW - New Session Requested... Success (Start Session Thread Started)",
"2018.07.26 10:35:07:1219: DAIW - Start Session ThreadSuccess (c0c2311a-b509-4e6e-a236-80e2d86f2647)"

我也尝试过:

return troposLogs.OrderBy(m => m.UserName != null).ThenBy(m => m.UserName);

哪个返回此顺序:

"2018.07.26 10:35:06:7889: 4d50b064-d269-4256-a187-82a3f9402735 - Client successfully got the transaction result",
"2018.07.26 10:35:06:7959: 4d50b064-d269-4256-a187-82a3f9402735 - Run Transaction Requested (SOCS)... Success",
"2018.07.26 10:35:06:9169: 4d50b064-d269-4256-a187-82a3f9402735 - Run Transaction Thread (SOCS)... Success",
"2018.07.26 10:35:06:9229: 4d50b064-d269-4256-a187-82a3f9402735 - Client successfully got the transaction result",
"2018.07.26 10:35:07:1229: c0c2311a-b509-4e6e-a236-80e2d86f2647 - Client Successfully Retrieved Session",
"2018.07.26 10:35:07:1429: 4d50b064-d269-4256-a187-82a3f9402735 - End Session Requested - Thread started",
"2018.07.26 10:35:06:9019: DAIW - New Session Requested... Success (Start Session Thread Started)",
"2018.07.26 10:35:07:1219: DAIW - Start Session ThreadSuccess (c0c2311a-b509-4e6e-a236-80e2d86f2647)"

在这种情况下,日期已接管。但是我需要会话ID才能保持在一起。

有人可以帮我吗?

这是一个对位日志的示例(我已经忽略了数据类型,只是向您展示它如何与字符串日志联系在一起)

var logString = "2018.07.26 10:35:06:7889: 4d50b064-d269-4256-a187-82a3f9402735 - Client successfully got the transaction result";
var log = new TroposLog();
log.Created = "2018.07.26 10:35:06:7889";
log.SessionId = "4d50b064-d269-4256-a187-82a3f9402735";
log.Message = "Client successfully got the transaction result";

上面的日志没有UserNameActionName

var logString = "2018.07.26 10:35:07:1219: DAIW - Start Session ThreadSuccess (c0c2311a-b509-4e6e-a236-80e2d86f2647)";
var log = new TroposLog();
log.Created = "2018.07.26 10:35:07:1219";
log.SessionId = "4d50b064-d269-4256-a187-82a3f9402735";
log.Message = "Start Session ThreadSuccess (c0c2311a-b509-4e6e-a236-80e2d86f2647)";
log.UserName= "DAIW";

此人没有ActionName。最后:

var logString = "2018.07.26 10:35:06:9169: 4d50b064-d269-4256-a187-82a3f9402735 - Run Transaction Thread (SOCS)... Success";
var log = new TroposLog();
log.Created = "2018.07.26 10:35:06:9169";
log.SessionId = "4d50b064-d269-4256-a187-82a3f9402735";
log.Message = "Run Transaction Thread (SOCS)... Success";
log.ActionName= "SOCS";

此人没有UserName。 我希望这有助于澄清

2 个答案:

答案 0 :(得分:2)

尝试:

troposLogs.OrderBy(m => m.UserName == null).ThenBy(m => m.UserName)

所以UserName == nulltrueUserName != nullfalse,并且bool值的顺序是false, true

您的错误是您先按UserName进行排序,然后按UserName进行排序“为空” ...但是第二种排序没有用(因为按UserName进行的排序已将{{1 }}和null)。您必须做相反的事情(如我所做的那样)。

测试:

not null

答案 1 :(得分:1)

您真正想要的是按SessionId对日志进行分组,然后按日期对每个组进行排序(我认为),因此您可以尝试以下操作:

        var troposLogs = new TroposLog[]
        {
            new TroposLog
            {
                Created = DateTime.Now, 
                UserName = null,
                SessionId = "4d50b064-d269-4256-a187-82a3f9402735",
                ActionName = null,
                Message = "Client successfully got the transaction result"
            },
            new TroposLog
            {
                Created = DateTime.Now,
                UserName = null,
                SessionId = "4d50b064-d269-4256-a187-82a3f9402735",
                ActionName = "SOCS",
                Message = "Run Transaction Requested (SOCS)... Success"
            },
            new TroposLog
            {
                Created = DateTime.Now,
                UserName = "DAIW",
                SessionId = "c0c2311a-b509-4e6e-a236-80e2d86f2647",
                ActionName = null,
                Message = "New Session Requested... Success (Start Session Thread Started)"
            },
            new TroposLog
            {
                Created = DateTime.Now,
                UserName = null,
                SessionId = "4d50b064-d269-4256-a187-82a3f9402735",
                ActionName = "SOCS",
                Message = "Run Transaction Thread (SOCS)... Success"
            },
            new TroposLog
            {
                Created = DateTime.Now,
                UserName = null,
                SessionId = "4d50b064-d269-4256-a187-82a3f9402735",
                ActionName = null,
                Message = "Client successfully got the transaction result"
            },
            new TroposLog
            {
                Created = DateTime.Now,
                UserName = "DAIW",
                SessionId = "c0c2311a-b509-4e6e-a236-80e2d86f2647",
                ActionName = null,
                Message = "Start Session ThreadSuccess (c0c2311a-b509-4e6e-a236-80e2d86f2647)"
            },
            new TroposLog
            {
                Created = DateTime.Now,
                UserName = null,
                SessionId = "c0c2311a-b509-4e6e-a236-80e2d86f2647",
                ActionName = null,
                Message = "Client Successfully Retrieved Session"
            },
            new TroposLog
            {
                Created = DateTime.Now,
                UserName = null,
                SessionId = "4d50b064-d269-4256-a187-82a3f9402735",
                ActionName = "",
                Message = "End Session Requested - Thread started"
            },
        };

        var orderedLogs = troposLogs.OrderBy(l => l.Created) // Just in case
                                    .GroupBy(l => l.SessionId)
                                    .OrderBy(g => g.FirstOrDefault().Created)
                                    .SelectMany(g => g)
                                    .ToList();

注意:要使此工作正常进行,您需要在填充模型时为所有日志条目分配正确的sessionId,例如'DAIW'用户的所有日志都应具有sessionId'c0c2311a-b509-4e6e-a236-80e2d86f2647',根据您的输入数据,这是正确的(这没什么大不了的)

希望这对您有帮助