2

伙计们,我有两个问题......首先我有一个 linq 查询搜索,它查询姓名和姓氏并从数据库返回结果。例如,如果我搜索 John,它将返回完美的 John travolta(我的问题是)如果我同时搜索 john travolta 的名字和姓氏,它会找不到结果?...帮助。

问题二:再次搜索时如何清除搜索结果?因为当我第二次搜索时,前一次搜索的结果是

public class SearchResults
        {
            public string Name { get; set; }
            public string Surname { get; set; }

            public SearchResults(string name, string surname)
            {
                Name = name;
                Surname = surname;
            }



 protected void btnSearch_Click(object sender, EventArgs e)
        {
            string search = txtSearch.Text.ToLower().TrimEnd();
            lsSearchResults = null;
            lsSearchResults = new List<SearchResults>();
            Repeater1.DataSource = null;
            lsSearchResults.Clear();
            Repeater1.DataSource = "";
        }

        if (string.IsNullOrWhiteSpace(txtSearch.Text))
            {
              lblResults.Text = "Please enter a password & Em@ilAddress";

                lsSearchResults.Clear();

                return;
            }

        else
        {
            var found = (from User in myDB.Memberships
                         where User.Name.ToLower().Contains(txtSearch.Text.ToLower()) ||
                         User.Surname.ToLower().Contains(txtSearch.Text.ToLower())
                         select new { User.Name, User.Surname });

            // validates items in search query if Exist
            if (!ChillZARdev.App_Code.Utilities.IsEmpty(found))
            {
                foreach (var user in found)
                {
                    lsSearchResults.Add(new SearchResults(user.Name, user.Surname));
                    // ls.Add(user.Name + " " + user.Surname);

                    // Response.Write(user);
                }
                Repeater1.DataSource = lsSearchResults;

                Repeater1.DataBind();

             }
          }
4

3 回答 3

4

Short solution would be to include firstname and surname together in the where clause:

var found = (from User in myDB.Memberships
             where User.Name.ToLower().Contains(txtSearch.Text.ToLower()) ||
             User.Surname.ToLower().Contains(txtSearch.Text.ToLower()) ||
             (User.Name + " " + User.Surname).ToLower().Contains(txtSearch.Text.ToLower())

However, this means a search for "Travolta john" wouldn't return any results.

If this matters you should split up the string by spaces, and search for each word.

List<string> searchSplit = txtSearch.Text.ToLower().Split(' ');

var found = (from User in myDB.Memberships
         where IsMatch(searchSplit, User)

private bool IsMatch(List<string> searchSplit, User User){
   return searchSplit.Count() ==
          searchSplit.Where(x => User.Name.ToLower().Contains(x) ||
                                 User.Surname.ToLower().Contains(x)).Count();
}

Ideally, as Steve B has commented, it would be best to implement a pre-built search engine which would require more tools for searching than a simple custom search would. But it depends on what your budget/requirements are.

于 2013-07-08T12:27:28.680 回答
1

A couple of months back, i was having the same problem, i was using singleton design pattern and the root problem was with the cache.. and for both of your problems there is one solution - you can try to clean the cache of a LINQ to SQL DataContext and you can do that by following way

public static void ClearCache(this DataContext context) 
 { 
    const BindingFlags FLAGS = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; 
    var method = context.GetType().GetMethod("ClearCache", FLAGS); 
    method.Invoke(context, null); 
 } 

and you can call that method

   public void DoWork() 
     { 
        //do work... 
        dataContext.ClearCache(); 
       //do more work... 
     } 

Hope it helps

于 2013-07-08T12:35:14.423 回答
1
var found = myDB.Memberships;
foreach(var word in txtSearch.Text)
{
    var word2=word;  //Must do this because word won't be right at execution
    found=found.Where(u=>u.Name.Contains(word2) || u.Surname.Contains(word2));
}

我删除了 .ToLower,因为列应该已经被标记为不区分大小写,除非您直接更改它,或者数据库的默认区域性更改。

于 2013-07-08T18:47:30.623 回答