我有一个场景,我有一个字典a,其中键存储为-'employeeId,cardnumber'
key value
'Id47835,12345', 'Emma,9843675867,Park Street'
'Id47800,4444', 'Sam,981275337,New Street'
'Id47866,41234', 'Pam,9812123337,Old Street'
'Id47866,03434', 'Jim,9866623337,Old Street'
'ID95455,22345', 'Robert,984735846,New Street'
雇员id和卡号是唯一的。
我必须与上面的字典比较一些值。我将从。csv文件中读取这些值。比较条件如下:
CSV文件:
EmpID CardNumber Name Phone Address
Id47835 12345 Emma 9843675867 Park Street ---- this user is present in dict A - same emp Id and same card number
Id47800 8888 Sam 981275337 New Street ----- this user is present in dict A - same emp Id and different card number
Id33333 41234 Pam 9812123337 Old Street ----- this user is present in dict A - different emp Id and same card number
ID94899 3434 Jim 9812123337 Old Street ---- this user is not present in dict A - different emp Id and different card number - leading zeroes are considered different.
当前,我正在执行以下操作:
检查hashtable值的任何部分是否包含特定字符串c#
c# -哈希表的部分搜索if (dictA.Keys.Cast<string>().Any(k => k.Contains(employeeID) || k.Contains(cardnumber)))
//then do something
通过这种方法,我能够检查上面提到的所有3个条件,但我的最后一个条件是失败的(前导零)
当我比较卡号3434和03434时,结果为真。当然,因为我使用的是contains
,而03434包含了3434。
他们有部分搜索字典的替代品吗?
这些是csv文件。
文件1 -字典是建立在这个
Name,PhoneNumber,AccessDetails,Address
Emma,987462534,ID47835|12345,Park Street
Sam,975323444,ID47800|4444,New Street
Jim,908796856,ID47866|41234,Old Street
Pam,985764567,ID94899|03434,Old Street
Robert,984735846,ID95455|22345,New Street
文件2
Name,PhoneNumber,AccessDetails,Address
Emma,987462534,ID47835|12345,Park Street
Sam,975323444,ID47800|8888,New Street
Jim,908796856,ID33333|41234,Old Street
Pam,985764567,ID94899|3434,Old Street
Access Details如employeeId|Card Number
。除了上述其他条件外……如果有一个用户的emp id和卡号不匹配,那么还有其他的场景给他。
可能有更有效的解决方案,但你的问题可以通过以下方法解决:
if (dictA.Keys.Cast<string>().Any(k => {
var parts = k.Split(',');
return parts[0] == employeeID || parts[1] == cardnumber
}))
对数据进行预处理要比在每次搜索时对数据进行拆分和比较要好得多。解决方案假设密钥的格式始终为'{id},{cardNumber}',没有空格、尾随空格等。
你真的需要做这样的事情:
var file1 =
File
.ReadLines(@"file1.csv")
.Select(x => x.Split(','))
.Skip(1)
.Select(xs => new
{
Name = xs[0],
PhoneNumber = xs[1],
EmpID = xs[2].Split('|')[0],
CardNo = xs[2].Split('|')[1],
Address = xs[3],
})
.ToArray();
var file2 =
File
.ReadLines(@"file2.csv")
.Select(x => x.Split(','))
.Skip(1)
.Select(xs => new
{
Name = xs[0],
PhoneNumber = xs[1],
EmpID = xs[2].Split('|')[0],
CardNo = xs[2].Split('|')[1],
Address = xs[3],
})
.ToArray();
var query =
from f1 in file1
from f2 in file2
where f1.EmpID == f2.EmpID || f1.CardNo == f2.CardNo
select f2;