Convert nested for-loops into single LINQ statement

后端 未结 5 1122
一生所求
一生所求 2021-02-08 03:23

can someone please help me turn this nested structure into a single LINQ statement?

        EventLog[] logs = EventLog.GetEventLogs();
        for (int i = 0; i          


        
5条回答
  •  清歌不尽
    2021-02-08 04:01

    I have this solution, and I'm assuming that remoteAccessLogs is of type List

    remoteAccessLogs.AddRange(
    
            from log in EventLog.GetEventLogs()
            from entry in log.Entries.Cast()
            select entry
        );
    

    Edit

    I forgot about the where clauses

    List remoteAccessLogs = new List();
    
    
        remoteAccessLogs.AddRange(
    
            from log in EventLog.GetEventLogs()
            where log.LogDisplayName.Equals("AAA")
            from entry in log.Entries.Cast()
            where entry.Source.Equals("BBB")
            select entry
        );
    

提交回复
热议问题