Home > Software design >  Partial search in Dictionaries
Partial search in Dictionaries

Time:12-24

I have a scenario where I have a dictionary A where key is stored as - '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'

EmployeeIds and Card numbers are unique.

I have to compare some values with the above dictionary. I will be reading those values from a .csv file. The comparison conditions are as follows.

CSV file:

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. 

Currently, I am doing the following from:

Check if any part of a hashtable value contains certain string c#

C# - Partial Search in Hashtables

if (dictA.Keys.Cast<string>().Any(k => k.Contains(employeeID) || k.Contains(cardnumber)))
//then do something

With this approach I am able to check all the 3 conditions mentioned above, but my last condition is failing (leading zeroes)

When I compare card number 3434 with 03434, it yields as true. Of course, because I am using contains and 03434 contains 3434.

Is their any alternative to partial search dictionaries?

CodePudding user response:

There are probably more efficient solutions but your problem can be solved by doing the following:

if (dictA.Keys.Cast<string>().Any(k => {
    var parts = k.Split(',');
    return parts[0] == employeeID || parts[1] == cardnumber
}))

It would be much better to preprocess the data than have to perform the split and compare on it for each search though. The solution assumes that the key will always be in the format '{id},{cardNumber}' with no blanks, trailing spaces, etc.

  • Related