我的搜索逻辑存在问题。当用户想要搜索区域"团队区域123"它会因空间而搜索姓氏。只是想看看我们怎么能告诉搜索它不是第一个姓氏搜索而你做错了?
public IEnumerable<User> Query()
{
if (!String.IsNullOrEmpty(query))
{
int ext = 0;
if (query.Contains("."))
{
return repository.SelectSearchByEmail(query.Trim());
}
else if (query.Contains(","))
{
var names = query.Split(',');
var firstName = names[0];
var lastName = names[1];
return repository.SelectSearchByFirstAndLastName(firstName, lastName);
}
else if (query.Contains(" "))
{
var names = query.Split(' ');
var firstName = names[0];
var lastName = names[1];
return repository.SelectSearchByFirstAndLastName(firstName, lastName);
}
else if (query.Contains("_"))
{
var names = query.Split('_');
var firstName = names[0];
var lastName = names[1];
return repository.SelectSearchByFirstAndLastName(firstName, lastName);
}
else if (int.TryParse(query, out ext))
{
return repository.SelectSearchByExt(ext);
}
else
{
return repository.SelectAllUserByQuery(query);
}
}
return new List<User>();
}
答案 0 :(得分:0)
尝试这样的事情:
计算字符串中的空格数
// For brevity use.
int spacesCount = query.Count(f => f == ' ');
// For speed use.
int spacesCount = 0;
foreach (char c in query)
{
if (c == ' ')
{
count++;
}
}
只有当有1个空格时才会落入其他地方,例如“名字姓氏”
所以改变
else if (query.Contains(" "))
到
else if (spacesCount == 1)
您的代码应如下所示:
public IEnumerable<User> Query()
{
if (!String.IsNullOrEmpty(query))
{
int ext = 0;
int spacesCount = query.Count(f => f == ' ');
if (query.Contains("."))
{
return repository.SelectSearchByEmail(query.Trim());
}
else if (query.Contains(","))
{
var names = query.Split(',');
var firstName = names[0];
var lastName = names[1];
return repository.SelectSearchByFirstAndLastName(firstName, lastName);
}
else if (spacesCount == 1)
{
var names = query.Split(' ');
var firstName = names[0];
var lastName = names[1];
return repository.SelectSearchByFirstAndLastName(firstName, lastName);
}
else if (query.Contains("_"))
{
var names = query.Split('_');
var firstName = names[0];
var lastName = names[1];
return repository.SelectSearchByFirstAndLastName(firstName, lastName);
}
else if (int.TryParse(query, out ext))
{
return repository.SelectSearchByExt(ext);
}
else
{
return repository.SelectAllUserByQuery(query);
}
}
return new List<User>();
}
答案 1 :(得分:0)
检查空格时缺少一个条件:
public IEnumerable<User> Query()
{
if (!String.IsNullOrEmpty(query))
{
int ext = 0;
if (query.Contains("."))
{
return repository.SelectSearchByEmail(query.Trim());
}
else if (query.Contains(","))
{
var names = query.Split(',');
var firstName = names[0];
var lastName = names[1];
return repository.SelectSearchByFirstAndLastName(firstName, lastName);
}
else if (query.Contains(" ") && query.Split(" ").Length ==2 ) //This Line
{
var names = query.Split(' ');
var firstName = names[0];
var lastName = names[1];
return repository.SelectSearchByFirstAndLastName(firstName, lastName);
}
else if (query.Contains("_"))
{
var names = query.Split('_');
var firstName = names[0];
var lastName = names[1];
return repository.SelectSearchByFirstAndLastName(firstName, lastName);
}
else if (int.TryParse(query, out ext))
{
return repository.SelectSearchByExt(ext);
}
else
{
return repository.SelectAllUserByQuery(query);
}
}
return new List<User>();
}
答案 2 :(得分:0)
如果我理解您的问题,那么您可以执行以下操作:
HasOptional(d => d.Car)
.WithOptionalDependent()
.Map(d => d.MapKey("CarId"));