我在.net中创建了一个C# MVC应用程序,其中列出了组织,目前数据库中有6000条记录(SQLServer)。组织表中的字段是:
当前的搜索是使用linq完成的,例如:
iList<Organisation> orglist = myOrgs.Where(x => x.Title.Contains('abc') ||
x.ContactPerson.Contains('abc') ||
x.Details.Contains('abc') ||
x.Keywords.Contains('abc'))
.OrderBy(x => x.Title).ToList();然后将结果按标题排序。这不合适。
如果有人搜索‘酒精支持’,我希望上面的结果在列表的首位。
我希望将结果按以下顺序排列:
寻找实现这一点的最佳方法的建议,或者如果有人知道有任何算法/库已经这样做了?
**更新**我现在正在寻找一个更简单的解决方案,请参阅以下链接:
发布于 2019-01-07 17:54:46
摘要:
- R01 | titles | Full matches |in order
- R02 | titles | Full matches |in any order
- R03 | titles | Any matches |
- R04 | keywords | Any matches |
- R05 | content | Full matches |
步骤01 : R01
linq2Sql 包含的这种用法将被转换为sql IN。
第二步: R02
步骤03 : R03
第04步: R04
第05步: R05
linq2Sql 包含的这种用法将被转换为sql IN。
步骤06 -将Id分组,忽略可复制的Id
我们将根据订单检索对id的基础进行排序
var ids = new Dictionary<int, int>();
foreach (var id in Ids1)
{
int val;
if (!ids.TryGetValue(id, out val))
{
ids.Add(id, ids.Count());
}
};
.
.第07步-重新订购
ids.OrderByDescending(o => o.Value)
.Select(s => s.Key) .ToArray();步骤08 -现在我们使用Oredred Id来获取数据
第09步完整代码
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp9
{
class Program
{
static void search(string search_query)
{
//////////////////////////////////////////////////
var terms = search_query.Split(' ');
//////////////////////////////////////////////////
var Ids1 = db.Orgs.
Where(w => w.Title.Contains(search_query))
.Select(s => s.Id).ToList();
var Ids2 = db.Database
.SqlQuery<int>(getWhere("Title", "AND"), terms)
.ToList();
var Ids3 = db.Database
.SqlQuery<int>(getWhere("Title", "OR"), terms)
.ToList();
var Ids4 = db.Database
.SqlQuery<int>(getWhere("Keywords", "OR"), terms)
.ToList();
var Ids5 = db.Orgs
.Where(w => w.Content.Contains(search_query))
.Select(s => s.Id).ToList();
var ordered_ids = reorderList(Ids1, Ids2, Ids3, Ids4, Ids5);
var OrderedData = db.Database.SqlQuery<Org>(getOrdered(ordered_ids)).ToList();
//////////////////////////////////////////////////
foreach (var item in OrderedData)
{
Console.WriteLine($"{item.Id} - {item.Title} - {item.ContactPerson } - {item.Keywords } - {item.Content }");
}
//////////////////////////////////////////////////
Console.ReadLine();
//////////////////////////////////////////////////
string getWhere(string _column, string _oprator)
{
var val = "Select Id From Orgs where ";
for (int i = 0; i < terms.Length; i++)
{
if (i > 0) val += @" " + _oprator + " ";
val += @" " + _column + " LIKE '%' + {" + i + "} +'%' ";
}
return val;
}
//////////////////////////////////////////////////
string getOrdered(int[] _ids_ordered)
{
var val = "Select * From Orgs where ";
val += " Id in ";
for (int i = 0; i < _ids_ordered.Length; i++)
{
if (i == 0) val += "( ";
if (i > 0) val += " , ";
val += _ids_ordered[i];
if (i == terms.Length - 1) val += " ) ";
}
val += " ORDER BY CASE id ";
for (int i = 0; i < _ids_ordered.Length; i++)
{
val += " WHEN " + _ids_ordered[i] + " THEN " + i;
}
val += " ELSE " + _ids_ordered.Length + " END ";
return val;
}
//////////////////////////////////////////////////
int[] reorderList(List<int> _Ids1, List<int> _Ids2,
List<int> _Ids3, List<int> _Ids4, List<int> _Ids5)
{
var idsDic = new Dictionary<int, int>();
var idsArr = new List<int>[5] { Ids1, Ids2, Ids3, Ids4, Ids5 };
for (int i = 0; i < 5; i++)
{
idsArr[i].ForEach(id =>
{
if (!idsDic.TryGetValue(id, out int val))
idsDic.Add(id, idsDic.Count());
});
};
var o_ids = idsDic.OrderByDescending(o => o.Value)
.Select(s => s.Key).ToArray();
return o_ids;
}
}
static Model1 db = new Model1();
static void Main(string[] args)
{
string search_quer = "Alcohol Support";
Console.WriteLine($"searching for {search_quer}");
search("Alcohol Support");
}
}
}注01 : Sql注入
注01.01 :问题
注01.02 : Microsoft文档

注01.03 :在本项目中


注01.04 :另一种格式
https://stackoverflow.com/questions/54074142
复制相似问题