Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Silverlight c#中搜索姓名识别_C#_Silverlight - Fatal编程技术网

在Silverlight c#中搜索姓名识别

在Silverlight c#中搜索姓名识别,c#,silverlight,C#,Silverlight,我有一个使用棱镜练习的silverlight应用程序;当前代码按名字、姓氏或性别进行搜索。关于名称,我想将代码改为3个字符,因为现在它正在搜索,只要找到一个字符,名称就会显示出来,这样您就可以看到问题。我可以调整代码,只选择那些与3个字符匹配的吗?让我们撇开一个少于3个的名字不谈,但是我们可以允许任何事情发生 using System; using System.Collections.Generic; using System.Linq; using System.Web; namespac

我有一个使用棱镜练习的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;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
命名空间PBM.Web.Classes
{
公共类搜索
{
公共静态IQueryable GetSearchQueryPatient(IQueryable pSearchQuery,Patient pPatient)
{
如果(!string.IsNullOrEmpty(pPatient.FirstName))
{
pSearchQuery=pSearchQuery.Where(item=>item.FirstName.Contains(pPatient.FirstName));
}
如果(!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)。然后是by(item=>item.LastName);
返回pSearchQuery;
}
}
}

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

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));
    }
}

那将是一个问题。除非我们可以说,如果姓氏超过2,则搜索3,否则执行旧搜索。