我正在尝试根据某些条件过滤对象列表。我已经看到一些在LINQ查询中使用三元运算符的帖子来完成此操作。

在此代码段的底部是我的LINQ查询。我希望它能够测试每个where,相反,它似乎只尝试第一个where并保留其余的。 List不包含我期望的数据。我究竟做错了什么?

    private ObservableCollection<LogEvent> m_LogEvents = ApplicationData.MainLogEntries.LogEvents;
    public ObservableCollection<LogEvent> LogEvents
    {
        get { return m_LogEvents; }
        set { m_LogEvents = value; RaisePropertyChanged("LogEvents"); }
    }

    private bool m_ViewDebugLogs = false;
    public bool ViewDebugLogs
    {
        get { return m_ViewDebugLogs; }
        set { m_ViewDebugLogs = value; RaisePropertyChanged("ViewDebugLogs"); FilterList(); }
    }

    private bool m_ViewErrorLogs = true;
    public bool ViewErrorLogs
    {
        get { return m_ViewErrorLogs; }
        set { m_ViewErrorLogs = value; RaisePropertyChanged("ViewErrorLogs"); FilterList(); }
    }

    private bool m_ViewInfoLogs = true;
    public bool ViewInfoLogs
    {
        get { return m_ViewInfoLogs; }
        set { m_ViewInfoLogs = value; RaisePropertyChanged("ViewInfoLogs"); FilterList(); }
    }

    private void FilterList()
    {
        List<LogEvent> selectedEvents = (from x in LogEvents
                                         where (ViewDebugLogs) ? x.Level == "Debug" : false
                                         where (ViewErrorLogs) ? x.Level == "Error" : false
                                         where (ViewInfoLogs) ? x.Level == "Info" : false
                                         select x).ToList();

    }


编辑:这样做的目的是如果启用了适当的布尔ViewDebugLogsViewErrorLogsViewInfoLogs或这三种的任意组合,则日志将通过适当的类型进行过滤。

我已经从答案中尝试了以下方法,但均未成功:

这仅得到Debug Logs

            List<LogEvent> selectedEvents = (from x in LogEvents
                                         where (ViewDebugLogs == true) ? x.Level == "Debug" : false
                                         || (ViewErrorLogs == true) ? x.Level == "Error" : false
                                         || (ViewInfoLogs == true) ? x.Level == "Info" : false
                                         select x).ToList();


这仅得到Debug Logs

            List<LogEvent> selectedEvents = (from x in LogEvents
                                         where (ViewDebugLogs == true) ? x.Level == "Debug" : true
                                         || (ViewErrorLogs == true) ? x.Level == "Error" : true
                                         || (ViewInfoLogs == true) ? x.Level == "Info" : true
                                         select x).ToList();


这什么也没有:

            List<LogEvent> selectedEvents = (from x in LogEvents
                                         where (ViewDebugLogs == true) ? x.Level == "Debug" : true
                                         where (ViewErrorLogs == true) ? x.Level == "Error" : true
                                         where (ViewInfoLogs == true) ? x.Level == "Info" : true
                                         select x).ToList();

最佳答案

你可以用

 List<LogEvent> selectedEvents = (from x in LogEvents
                           where (ViewDebugLogs && x.Level == "Debug")
                              || (ViewErrorLogs && x.Level == "Error")
                              || (ViewInfoLogs  && x.Level == "Info")
                           select x).ToList();


(我认为)这一点要清楚得多。请注意表达式部分周围的( ):我想确保对AND和OR求值的顺序。

10-07 23:17