当前8位数字匹配时如何获取行

时间:2019-06-03 03:50:53

标签: c#

我需要获取仅在前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'

2 个答案:

答案 0 :(得分:1)

尝试一下

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.

enter image description here

希望有帮助,

答案 1 :(得分:0)

尝试下面的代码

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

string[] filteredData =  column1Data.Where(str1 => 
   column2Data.Any(
       str2 => str2.Substring(0, 8).Equals(str1.Substring(0, 8))
       && !str2.Substring(8).Equals(str1.Substring(8))
       )
   ).ToArray();