Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

So sánh hai List và thêm khác biệt vào List thứ ba trong C#


Tương tự bài trước: dùng Except() lấy difference, lưu vào IEnumerable/List thứ ba.

Ví dụ

using System;
using System.Collections.Generic;
using System.Linq;
public class Demo {
    public static void Main() {
        List<string> list1 = new List<string>(); list1.Add("A"); list1.Add("B"); list1.Add("C"); list1.Add("D");
        List<string> list2 = new List<string>(); list2.Add("C"); list2.Add("D");
        IEnumerable<string> list3 = list1.Except(list2);
        foreach (string v in list3) Console.WriteLine(v); // A, B
    }
}