使用LINQ宽恕/模糊搜索 [英] Forgiving/Fuzzy search with LINQ

查看:143
本文介绍了使用LINQ宽恕/模糊搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实施打击,我继承了一个数据库的搜索。要求规定,用户必须能够按名称搜索对象。不幸的是,一个对象可以有多个与之相关的名字。例如:




  ID名称
约翰和简·多伊
2富McFoo
3嘘McBoo




这是很容易实现一个搜索时,一个名字在每个记录存在:

  VAR对象=从X在db.Foo 
,其中x.Name.Contains(富McFoo)
选择x;



然而,当多个名称存在,这种做法是行不通的。



问:是否有可能写一个搜索方法,将返回记录中的一个(John和Jane Doe的)当有人使用的搜索词李四李四


解决方案

这会受伤的表现,但如何对这种快速之一:

 的String []过滤器=李四.Split(新[] { ''}); 
变种在db.Foo
=物体从x其中filters.All(F => x.Name.Contains(F))
选择X;



这似乎回到你所期望的。现在,你会调整它表现不错,当你有也创下李四,以及John和Jane Doe的。



这是否对你的工作?


I'm attempting to implement a search against a DB that I inherited. The requirement states that the user must be able to search for an object by name. Unfortunately, an object could have multiple names associated with it. For example:

ID    Name
1     John and Jane Doe
2     Foo McFoo
3     Boo McBoo

It's easy enough to implement a search when a single name exists in each record:

var objects =  from x in db.Foo
               where x.Name.Contains("Foo McFoo")
               select x;

However, when multiple names exist, that approach does not work.

Question: Is it possible to write a search method that would return record one (John and Jane Doe) when someone uses the search terms John Doe or Jane Doe?

解决方案

This would hurt performance, but how about this quick one:

string[] filters = "John Doe".Split(new[] {' '});
var objects =  from x in db.Foo
               where filters.All(f => x.Name.Contains(f))
               select x;

It seems to return what you would expect. Now you would tune it to behave nice when you have also a record "John Doe" as well as "John and Jane Doe".

Does this work for you?

这篇关于使用LINQ宽恕/模糊搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆