在 Silverlight C# 中搜索名称识别
我有一个使用 Prism 实践的 silverlight 应用程序;当前代码按名字或姓氏或性别进行搜索。关于名称,我想将代码更改为 3 个字符之类的内容,因为现在它正在搜索,只要找到一个字符,名称就会显示,以便您可以看到问题,我可以在此处调整代码以仅选择那些具有3 个字符匹配?让我们先不管名称少于 3 的问题,但我们可以允许任何东西。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace PBM.Web.Classes
{
public class Search
{
public static IQueryable<Patient> GetSearchQueryPatient(IQueryable<Patient> pSearchQuery, Patient pPatient)
{
if (!string.IsNullOrEmpty(pPatient.FirstName))
{
pSearchQuery = pSearchQuery.Where(item => item.FirstName.Contains(pPatient.FirstName));
}
if (!string.IsNullOrEmpty(pPatient.LastName))
{
pSearchQuery = pSearchQuery.Where(item => item.LastName.Contains(pPatient.LastName));
}
if (pPatient.Gender.HasValue && pPatient.Gender.Value > 0)
{
pSearchQuery = pSearchQuery.Where(item => item.Gender.Value == pPatient.Gender.Value);
}
pSearchQuery = pSearchQuery.OrderBy(item => item.FirstName).ThenBy(item => item.LastName);
return pSearchQuery;
}
}
}
I have a silverlight app using Prism practices; the current code does a search by first name or last name or gender. regaring the names, I would like to alter the code to somethng like 3 characters because now it is searching as long as one character is found the name will display so you can see the issue, can I adjust the code here to only select those with a 3 character match? lets leave alone the issue of a name with less than 3 but we can allow anything there then.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace PBM.Web.Classes
{
public class Search
{
public static IQueryable<Patient> GetSearchQueryPatient(IQueryable<Patient> pSearchQuery, Patient pPatient)
{
if (!string.IsNullOrEmpty(pPatient.FirstName))
{
pSearchQuery = pSearchQuery.Where(item => item.FirstName.Contains(pPatient.FirstName));
}
if (!string.IsNullOrEmpty(pPatient.LastName))
{
pSearchQuery = pSearchQuery.Where(item => item.LastName.Contains(pPatient.LastName));
}
if (pPatient.Gender.HasValue && pPatient.Gender.Value > 0)
{
pSearchQuery = pSearchQuery.Where(item => item.Gender.Value == pPatient.Gender.Value);
}
pSearchQuery = pSearchQuery.OrderBy(item => item.FirstName).ThenBy(item => item.LastName);
return pSearchQuery;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

如果我正确阅读了您的要求和示例代码,只需在您的测试中添加长度检查就可以了:
这确实意味着如果名称少于 3 个字符,它根本不匹配,所以您想要做的是然后检查此搜索是否返回任何内容,如果没有返回,则进行简单的任意长度搜索:
If I've read your requirement and sample code correctly, simply add a length check to your tests should work:
It does mean that if the name is less than 3 characters it won't match at all, so what you want to do is then check if this search returned anything and if not do the simple any length search: