我有一个MVC.NET应用程序,它有多个过滤器。我的数据库有很多关于汽车的信息。
我想实现用户可以输入的输入字段 “丰田雅力士1.6汽油”并获得与此相匹配的结果。问题是 在后端解析这个。我创建了一种解析方法但涉及用户指定键,例如“make”,“model”。
目前我的网站可以执行以下操作:“make = toyota,model = yaris,engine_size = 1.6,fueltype = Petrol”。如您所见,这对用户来说非常复杂。但我需要这些键来获取后端的值并将它们与正确的查询匹配。
<input class="form-control input-sm" name="filterPageSearchModel.GlobalSearchFilter" data-toggle="popover" data-html="true" id="PartCarSearchField"
data-placement="left" title="Example"
data-content="<a id='make' onclick='AppendText(this.id)'>make=</a> audi,
<a id='model' onclick='AppendText(this.id)'> model=</a> a4,
<a id='enginesize' onclick='AppendText(this.id)'> enginesize=</a> 1.6,
<a id='ribs' onclick='AppendText(this.id)'> ribs=</a> 7,
<a id='teeth' onclick='AppendText(this.id)'> teeth=</a> 9,
<a id='voltage' onclick='AppendText(this.id)'> voltage=</a> 12,
<a id='amp' onclick='AppendText(this.id)'> amp=</a> 34 a,
<a id='power' onclick='AppendText(this.id)'> power=</a>2.0kw,
<a id='fueltype' onclick='AppendText(this.id)'> fueltype=</a>petrol"/>
public IQueryable<PartMMi>GlobalSearchItems(string SearchValues)
{
Dictionary<string,string> ConvertedValues = ValuesConverter(SearchValues);
AutoPartDataClassesDataContext contextAp = new AutoPartDataClassesDataContext();
InternalDbDataClassesDataContext Context = new InternalDbDataClassesDataContext();
List<PartMMi> results = new List<PartMMi>();
var resultMMi = Context.mmis.AsQueryable();
foreach (var value in ConvertedValues)
{
if (LibraryOfParameters.ContainsKey(value.Key)){
LibraryOfParameters[value.Key] = value.Value;
}
}
if (LibraryOfParameters["make"].Length>0)
resultMMi = resultMMi.Where(x => x.car.make.Contains(LibraryOfParameters["make"]));
if (LibraryOfParameters["model"].Length > 0)
resultMMi = resultMMi.Where(x => x.car.model.Contains(LibraryOfParameters["model"]));
if (LibraryOfParameters["enginesize"].Length > 0)
resultMMi = resultMMi.Where(x => x.car.engine_size == Convert.ToDouble(LibraryOfParameters["enginesize"]));
private Dictionary<string, string> LibraryOfParameters = new Dictionary<string, string>()
{
{ "make", ""},
{ "model", ""},
{ "enginesize", ""},
{ "ribs", ""},
{ "teeth", ""},
{ "voltage", ""},
{ "amp", ""},
{ "power", ""},
{"fueltype","" }
};
public Dictionary<string,string> ValuesConverter(string OriginalSearchValues)
{
List<string> splitSearchString = OriginalSearchValues.Split(',').ToList();
Dictionary<string, string> SearchParameters = new Dictionary<string, string>();
foreach (string item in splitSearchString)
{
string temp1 = item.Split('=').FirstOrDefault();
string temp2 = item.Split('=').LastOrDefault();
SearchParameters.Add(temp1.ToLower().Trim(), temp2.Trim());
}
return SearchParameters;
}