如何通过输入单个文本框输入名称,姓氏,ssn的搜索机制?

时间:2016-01-14 13:29:57

标签: .net regex

我写了一个搜索机制。它工作得很好。但我想开发它来搜索更准确。怎么做?

假设我写了名字和姓氏以及ssn或tc(在火鸡中),我只等待一条记录,但我的算法带来了更多记录。如何改进它?我的基本算法如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
class Program
{
    private static void Main(string[] args)
    {

            Person p1 = new Person("John",              "Smith",    "28249690820");
            Person p2 = new Person("John Jack",         "Smith",    "28249690821");
            Person p3 = new Person("Ahmet",             "Willi",    "28249690827");
            Person p4 = new Person("John Brat",         "Willi",    "28249690829");
            Person p5 = new Person("Angelina",          "Smith",    "28249690831");
            Person p6 = new Person("Jack",              "Smith",    "28249690834");
            Person p7 = new Person("Jack Smith",        "",         "28249690837");
            Person p8 = new Person("John Jack Smith",   "",         "28249690835");
            Person p9 = new Person("John Brat",         "Williams", "28249690839");

            List<Person> l = new List<Person>();

            l.Add(p1);
            l.Add(p2);
            l.Add(p3);
            l.Add(p4);
            l.Add(p5);

            string[] filterArray = GetFilterGroup("John Smith 28249690820").ToArray();//just one record must come 

            string filterString = "";

            if (filterArray.Length == 0)
            {
                filterString = ".";
            }
            else
            {
                foreach (var obj in filterArray)
                {
                    filterString += obj + "|";
                }
                filterString = filterString.Substring(0, filterString.Length - 1);
            }

            Regex searchTerm = new Regex(@"("+filterString+")");

            var queryMatchingFiles = (from a in l
                 where (searchTerm.IsMatch(a.Name) || searchTerm.IsMatch(a.Surname)||searchTerm.IsMatch(a.Tc))
                 select new
                 {
                     a
                 }).ToList();


            foreach (var x in queryMatchingFiles)
            {
                Console.WriteLine("Name : "+x.a.Name+"\t\t"+"Surname : "+x.a.Surname);
            }
            Console.ReadKey();
        }

    public static IEnumerable<string> GetFilterGroup(string str)
    {
        var items = str.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);

        var groups = new List<string>(items);


        for (int i = 1; i < items.Count(); i++)
        {
            groups.Add(string.Join(" ", items.Where((s, idx) => idx <= i)));
        }

        return groups;
    }
}

 class Person
 {
     private string name;
     private string surname;
     private string tc;

     public Person(string name, string surname, string tc)
     {
         this.name = name;
         this.surname = surname;
         this.tc = tc;
     }

     public string Name
     {
         get { return name; }
         set { name = value; }
     }

     public string Surname
     {
         get { return surname; }
         set { surname = value; }
     }

     public string Tc
     {
         get { return tc; }
         set { tc = value; }
     }
 }

}

[enter image description here enter image description here

0 个答案:

没有答案