在 Silverlight C# 中搜索名称识别

发布于 01-05 23:19 字数 1248 浏览 2 评论 0原文

我有一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮光沉寂2025-01-12 23:19:52

如果我正确阅读了您的要求和示例代码,只需在您的测试中添加长度检查就可以了:

if (!string.IsNullOrEmpty(pPatient.FirstName) && pPatient.FirstName.Length > 2)
{
    pSearchQuery = pSearchQuery.Where(item => item.FirstName.Contains(pPatient.FirstName));
}

这确实意味着如果名称少于 3 个字符,它根本不匹配,所以您想要做的是然后检查此搜索是否返回任何内容,如果没有返回,则进行简单的任意长度搜索:

if (!string.IsNullOrEmpty(pPatient.FirstName))
{
    // First look for a 3 or more character match
    if (pPatient.FirstName.Length > 2)
    {
        pSearchQuery = pSearchQuery.Where(item => item.FirstName.Contains(pPatient.FirstName));
    }
    // If didn't find anything do the simple search
    if (!pSearchQuery.Any())
    {
        pSearchQuery = pSearchQuery.Where(item => item.FirstName.Contains(pPatient.FirstName));
    }
}

If I've read your requirement and sample code correctly, simply add a length check to your tests should work:

if (!string.IsNullOrEmpty(pPatient.FirstName) && pPatient.FirstName.Length > 2)
{
    pSearchQuery = pSearchQuery.Where(item => item.FirstName.Contains(pPatient.FirstName));
}

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:

if (!string.IsNullOrEmpty(pPatient.FirstName))
{
    // First look for a 3 or more character match
    if (pPatient.FirstName.Length > 2)
    {
        pSearchQuery = pSearchQuery.Where(item => item.FirstName.Contains(pPatient.FirstName));
    }
    // If didn't find anything do the simple search
    if (!pSearchQuery.Any())
    {
        pSearchQuery = pSearchQuery.Where(item => item.FirstName.Contains(pPatient.FirstName));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文