我可以像这样从两个texbox获得不匹配的值吗?

时间:2019-05-21 06:27:58

标签: c#

我可以获得已经匹配的值。但是我无法获得无与伦比的价值。如何使其工作。给我一些建议吧

private void button13_Click(object sender, EventArgs e)
        {
            String a = textBox3.Text;
            String b = textBox4.Text;
            string test = "";

            string[] titles1 = a.Split(new[] { ';', '\t', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
            string[] titles2 = b.Split(new[] { ';', '\t', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);

            var duplicates = titles1.Distinct().Concat(titles2.Distinct()).GroupBy(title => title).Where(possibleDuplicates => possibleDuplicates.Skip(1).Any()).Select(duplicate => duplicate.First());

            int i = 0;

            foreach (string duplicate in duplicates)
            {
                //test = test + duplicate + "\r\n";
                System.Data.DataTable table = new System.Data.DataTable();
                table.Columns.Add("Missed call");

                table.Rows.Add(duplicate);
                Excel_read.DataSource = table;
            }
        }

3 个答案:

答案 0 :(得分:3)

这可能对您有帮助

var result = titles1.Except(titles2).Concat(titles2.Except(titles1)).ToArray();

答案 1 :(得分:1)

这种方式:

List<string> list1 = new List<string> { "1", "2", "3", "4", "5", "7", "9"};
List<string> list2 = new List<string> { "1", "2", "3", "4", "5", "6", "8" };
List<string> list3 = list1;
list3.AddRange(list2);
list3 = list3.GroupBy(x => x).Where(x => x.Count() == 1).Select(x => x.Key).ToList();

答案 2 :(得分:0)

要找到差异,您可以使用以下代码(它是即时编写的,我并不是说它是一种性能优化的算法,只是一个想法,可以解决您的问题):

var list1 = new List<int> {1, 2, 3, 5};
var list2 = new List<int> {1, 2, 4, 5, 6};

var differences = list1.Where(v => !list2.Contains(v)).Concat(
                  list2.Where(v => !list1.Contains(v)));

// Or this compact version:
// var differences = list1.Except(list2).Concat(list2.Except(list1));

IEnumerable的差异将包含以下结果:

  

{3,4,6}