c# - LINQ 搜索相似字符串(名称)

标签 c# linq

关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。












想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。

8 个月前关闭。




Improve this question




例如名字是

  • 阿卜杜拉
  • 阿卜杜拉
  • 阿卜杜拉
  • 阿卜杜拉
  • 阿卜杜拉
  • 阿卜杜勒
  • 阿卜杜拉

  • 现在为此我将在 C# 中创建一个 Linq 查询,例如字符串是 textString = "abdolah"结果我期待所有这些。
    var data = db.TableList.where(a=>a.Name.Contains(textString).ToList();
    
    所以问题是如何或是否有任何内置库用于比较 .Net 中的名称

    最佳答案

    正如我上面所建议的,最快的入门方法是尝试我提到的库之一。
    这是使用 https://github.com/JakeBayer/FuzzySharp 在 2 分钟内编写的程序:
    结果:
    enter image description here
    代码:

    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 包:https://www.nuget.org/packages/FuzzySharp/2.0.2
    编辑:
    我在这里试过你的例子,我得到了 100%(显然)
    enter image description here

    关于c# - LINQ 搜索相似字符串(名称),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66347591/

    相关文章:

    c# - Linq to Entities——在 where 子句中有 Guid 会抛出错误

    c# - 检索设备类 GUID 或驱动程序 key

    c# - SharpSVN 中工作目录的当前版本

    c# - CancellationTokenSource 未按预期运行

    c# - 如果使用 Entity Framework 与列表中的任何匹配

    Linq 到实体 Left Join

    c# - 电话号码标准化 : Any pre-existing libraries?

    c# - 休眠Linq

    c# - 如何使用 LINQ 合并两个列表?

    c# - 无法获取 xml 文件中根元素的子元素