自动完成匹配多列 C# 的 Linq 查询

Linq Query For AutoComplete Match With Multiple Columns C#

我正在我的博客网站上实现搜索功能,每个博客可能包含标题、别名、post 内容等,应该像这样工作,

目标

When user type a keyword it should search for that string contains in any of 3 column in Sql Server in efficient way.

灵感来源

Source

我在用什么

 db.postTBs.Where(a => a.isApproved == true && a.isShow == true && a.pageKeyword.Contains(query) || a.pageDescribtion.Contains(query) || a.pageTitle.Contains(query) || a.postContent.Contains(query)).Select(a => a.title).ToList();

但我知道这不是获得结果的好方法,此查询搜索这 3 列的特定字符串长度但是当我有长字符串时

Only buy from reputable dealers. Any reputable dealer will only buy directly from the company or from a trusted distributor. Check the price. Does the price look too good to be true? It probably is. While there are many honest dealers on these sites, Amazon and eBay sellers are the biggest culprits when it comes to selling fake strings. Check the seller’s Amazon or eBay stores to verify who they are. If someone’s price is drastically lower than what you’ve been seeing, there’s a good chance this is not a legitimate string being sold. Buy your instrument from a reputable dealer too! Many cheap in

然后我搜索

Amazon and eBay

结果不包含实际post

但是这个匹配

On

我还在 3 列上应用了 Non cluster

请帮助如何使用 linq 查询 c# 使搜索高效准确

如果您准备在项目中使用第三方 dll,请访问 here

只需下载nuget包Install-Package NinjaNye.SearchExtensions

从link你可以使用

1) 包含: 在多个属性中搜索单个搜索词

var result = queryableData.Search(x => x.Property1,
                                  x => x.Property2,
                                  x => x.Property3)
                          .Containing("searchTerm");

2) ContainingAll: 搜索所有搜索词存在于多个属性中的位置

var result = queryableData.Search(x => x.Property1,
                                  x => x.Property2,
                                  x => x.Property3)
                          .ContainingAll("search", "term");

3) EqualTo: 搜索多个属性中的任何一个等于单个搜索词的位置

var result = queryableData.Search(x => x.Property1,
                                  x => x.Property2,
                                  x => x.Property3)
                          .EqualTo("searchTerm");

4) StartsWith: 搜索多个属性中的任何一个以单个搜索词开头的位置

var result = queryableData.Search(x => x.Property1,
                                  x => x.Property2,
                                  x => x.Property3)
                          .StartsWith("searchTerm");

5) EndsWith: 搜索多个属性中的任何一个以单个搜索词结尾的位置

var result = queryableData.Search(x => x.Property1, 
                                  x => x.Property2,
                                  x => x.Property3)
                          .EndsWith("searchTerm");