首页 > 解决方案 > 当他们的前 8 位数字匹配时如何获取行比较

问题描述

当只有前 8 位数字匹配时,我需要获取从 2 列进行比较的值

这是一些代码

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

var Result = titles1.Union(titles2).Except(titles2).ToArray();

For Example:

Column-1                 Column-2
'89118432 20190602'      '89115496 20190602'
'89114023 20180602'      '89114023 20180602'
'89110101 20190602'      '89118432 20170602'


 It's value i need 
'89118432 20190602'

标签: c#

解决方案


尝试这个,

string[] column1 = new[]
{
    "89118432 20190602",
    "89114023 20180602",
    "89110101 20190602"
};
string[] column2 = new[]
{
    "89115496 20190602",
    "89114023 20180602",
    "89118432 20170602"
};

// first by using union, making them as a single array
List<IGrouping<string, string>> dataList = column1.Union(column2)
                     .GroupBy(x => string.Join("", x.Take(8))).Where(x => x.Count() > 1).ToList();
// then by using Take extension, we took first 8 character. By Where extension we searched over array that 8 characters if exists more then 1.

在此处输入图像描述

希望有所帮助,


推荐阅读