开发者

Does LINQ have any built-in search which supports search-machine-like searching?

开发者 https://www.devze.com 2022-12-13 08:07 出处:网络
If I have a 开发者_运维知识库List<string> and want to do a standard search through it, I can use a LINQ statement like this:

If I have a 开发者_运维知识库List<string> and want to do a standard search through it, I can use a LINQ statement like this:

(from t in tasks
 where searchTerms.All(term => t.ToUpper().Contains(term.ToUpper()))
 select t).ToList();

But if I want to support standard search-engine-like syntax to handle phrases such as:

contract
contract customer
jim customer
"Jim Smith" customer

then I need to start rolling my own custom search method. In addition as Jon Skeet mentioned here, you have to be careful with comparing with ToUpper() with different culture settings, and if you are in a web environment, you have many issues with encoding and searching-for-encoded-characters issues, etc.

Is there not a .NET or LINQ solution which handles search-machine-like searches, e.g. instead of Contains() something like ConstainsSearchTerms()?


If you are using LINQ2SQL, you can use the SqlMethods.Like methods to generate a LIKE like used in SQL.

Else on LINQ2Objects, just use regex.

Regarding ToUpper. This is problematic, the better solution is to use string case folding, but unfortunately .NET only supports the simple model :(


It's not LINQ, but you may want to look into Lucene.NET. It actually is a search engine, and a pretty good one, too.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号