简体   繁体   中英

How can I add elements from one collection that do not exist in a second collection to a third collection?

I have 3 generic lists:

List<string> input
List<string> compareTo
List<string> results

I'd like to take the list of input and compare each value to the compare list, and if it doesn't exist add it to the results list.

你有什么理由不能只使用LINQ吗?

List<string> results = input.Except(compareTo).ToList();
compareTo.ForEach( x => {

    if(!input.Contains(x))results.Add(x); 


    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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