【发布时间】:2020-12-12 18:06:42
【问题描述】:
我有一份产品清单。 当我在使用 linq 的产品页面中时,我想选择一些类似的产品。 例如,我在“美国英语文件预中级”页面。 我在 db 中有一些产品,如下所示:
- 美式英语文件预中级
- 美式英语文件中级
- 美式英语文件高级版
- 美式英语文件高级版
- 美式英语文件初学者
我想做什么:
- 将“美式英语文件预中级”拆分为字符串[]。
- 我为 string[] 的大小定义了 int totalCount。
- 过滤长度大于 3 的单词(以消除一些单词,例如 'the' 、'and'、'or' 、...)
- 那么我想选择标题中至少有(2 个或 totalCount-1)个常用词和拆分词的产品。
- 以下是我用来测试具有两个列表的解决方案的内容。但我不喜欢使用 2 列表。
List<string> list = new List<string>();
List<string> list2 = new List<string>();
list.Add("english file pre-intermediate 2 the");
list.Add("english file intermediate 2 the");
list.Add("english file pre-advanced 2 the");
list.Add("english file advanced 2 the");
list.Add("english file beginner 2 the");
list.Add("english file");
list.Add("english file beginner 2 the");
list.Add("english file");
var words = textBox1.Text.Split(' ');
label1.Text = words.Length.ToString();
string res = "";
foreach (var item in words)
{
if (item.Length>3)
{
res = res + "-";
}
}
int total = words.Where(p => p.Length > 3).Count();
var fwords = words.Where(p => p.Length > 3).OrderBy(p=>p);
foreach (var item in list)
{
int i = 0;
int j = 0;
foreach (var w in fwords)
{
if (item.ToLower().Contains(w.ToLower()))
{
j++;
}
else
{
i--;
}
}
if (i>=-1 && j>=2)
{
list2.Add(item);
}
}
var res2 = "";
foreach (var item in list2)
{
res2 = res2 + item + "---";
}
label2.Text = res2;
【问题讨论】:
-
尝试模糊匹配,例如github.com/JakeBayer/FuzzySharp
-
var res = words.Where( w => w.Length> 3).Aggregate("-",(current, next)=> current + "-" ); -
int total = res.Length;
标签: c# asp.net sql-server linq model-view-controller