Вопрос

In my program, I'm trying to see if the user input is equal to the value in the database. However, the user input might be in a different order from the value in the database.

For example: User input

 A,C,B,E,D,F,G

Database value

 A,B,C,E,F,G,D

Therefore, when I compare user input to the database value, the strings will not match, even though the user input will have the appropriate responses. My question is whether or not it is possible to compare the user input to all possible permutations of the database value. The user input and database value may be of variable lengths. If there is one match, then show a messagebox saying "Correct". If there are no matches, then show a messagebox saying "Incorrect".

The user input is in a string, and the database value is also a string.

Thanks for your help.

  • each item is unique
Это было полезно?

Решение

The HashSet class has a SetEquals method that will determine if two sequences represent the same set of items (sets are unordered).

public static bool SetEquals<T>(this IEnumerable<T> first
    , IEnumerable<T> second
    , IEqualityComparer<T> comparer = null)
{
    comparer = comparer ?? EqualityComparer<T>.Default;
    return new HashSet<T>(second).SetEquals(first);
}

Другие советы

Best of course would be to have the values not in one field, but in a linked table.

Second best would be to use a split function (several options there in SQL Server) and use that to emulate the linked table, so you could still join on that.

A third way would be, not to generate all permutations (lots of overhead there), but to search with a mask on on the different values e.g.

   where ',' + [YourDataBaseField]  + ',' like '%,A,%' 
   AND ',' + [YourDataBaseField]  + ',' like '%,C,%' 
   --etc

The ',' +[YourDataBaseField] + ',' construction is to prevent having to use 3 statements, one for the start of the string, one for internal values and one for the end value. The above is assuming % is the wildcard character. Splitting the input string to generate the sql should be fairly straight forward.

If your problem is that the user makes error while typing you can implement modified Levenshtein - algorithm which was invented to improve spell-checkers accuracy(Damerau–Levenshtein wiki: http://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance). This algorithm gives you edit distance between two strings (so for example "cat" and "kbat" differs by two - add "k" at beginning of "cat" and change "c" to "b"). Experiment with edit-cost (for instance set adjacent exchange cost as 0 and substition cost for 1 etc.) - and if user-typed string and string in database differs at most by 0 "cost" (choose experimentally) then you could consider strings equal.

Get the hash code for each input string and add them up. Do the same for database values. Compare the two, if they match then all the fields are the same.

The String object has a GetHashCode() method that can be called and returns an integer.

Good luck, Z

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top