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

问题描述

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



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宽恕/模糊搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 12:33