LINQ search similar string(names)

Viewed 162

for example the names are

  • abdulah
  • abdullah
  • abdola
  • abdollah
  • S abdullah
  • abdul
  • aabdullah

Now for this I will create a Linq query in C#, the string for example is textString = "abdolah" in result I expect all of them.

var data = db.TableList.where(a=>a.Name.Contains(textString).ToList();

so the question is how or is there any built in library for comparing names in .Net

1 Answers

As I suggested above, fastest way to get started is to try one of the libraries I mentioned.

Here's a program wrote in 2 minutes using https://github.com/JakeBayer/FuzzySharp:

Result:

enter image description here

Code:

using System;
using System.IO;
using FuzzySharp;
using FuzzySharp.PreProcess;

namespace zzzzzzzz
{
    internal static class Program
    {
        private static void Main(string[] args)
        {
            var referenceName = "Abdallah";
            var referenceGoal = 80;

            var names = @"
abdulah
abdullah
abdola
abdollah
S abdullah
abdul
aabdullah";

            using (var reader = new StringReader(names))
            {
                while (true)
                {
                    var line = reader.ReadLine();

                    if (line == null)
                        break;

                    var ratio = Fuzz.Ratio(referenceName, line, PreprocessMode.Full);
                    var success = ratio >= referenceGoal;

                    Console.Write($"Current = '{line}', Ratio = {ratio}, Result = ");

                    var color = Console.ForegroundColor;
                    Console.ForegroundColor = success ? ConsoleColor.Green : ConsoleColor.Red;
                    Console.Write($"{(success ? "PASS" : "FAIL")}{Environment.NewLine}");
                    Console.ForegroundColor = color;
                }
            }
        }
    }
}

NuGet package of the library: https://www.nuget.org/packages/FuzzySharp/2.0.2

Edit:

I've tried your example here and I got 100% (obviously)

enter image description here

Related