使用音译在集合中实现搜索

时间:2012-08-11 14:13:45

标签: c# linq windows-phone-7 search

我需要从一个单词的开头搜索集合。

简单收集{Ivan Smith,Anthony Clark,Mike Shinoda}

当用户输入“s”时,我需要显示{Ivan Smith,Mike Shinoda}

这种方法应该有效,但事实并非如此。

     private void SortFriends(object sender, TextChangedEventArgs e) {
          var text = (sender as WatermarkTextBox).Text;
          if (text != string.Empty) {
            text = text.ToLowerInvariant();
            var translatedColl = FriendCollectionTranslated.Where(i => i.FirstName.StartsWith(text) || i.LastName.StartsWith(text) ||
                                                i.FirstNameT.StartsWith(text) ||
                                                i.LastNameT.StartsWith(text));

            var newCollection = ((App)App.Current).CollectionFriends.Where(i => translatedColl.First(n => n.Uid == i.Uid) != null);

            friendsBox.ItemsSource = newCollection;
          }
          else friendsBox.ItemsSource = ((App)App.Current).CollectionFriends;
        }
    public class HumanTranslated {
          public string Uid { get; set; }
          public string FirstName { get; set; }
          public string LastName { get; set; }
          public string FirstNameT { get; set; }//Translit
          public string LastNameT { get; set; }//Translit
          public bool IsChecked { get; set; }
        }
private void PrepareCollection()//Ретранслируем наши данные для быстрого поиска
    {
      foreach (var t in ((App)App.Current).CollectionFriends) {
        var item = new HumanTranslated {
          Uid = t.Uid,
          LastNameT = ToTranslit(t.LastName.ToLowerInvariant()),
          FirstNameT = ToTranslit(t.FirstName.ToLowerInvariant()),
          FirstName = t.FirstName.ToLowerInvariant(),
          LastName = t.LastName.ToLowerInvariant()
        };
        FriendCollectionTranslated.Add(item);
      }
    }
public class Human {
          public string Uid { get; set; }
          public string FirstName { get; set; }
          public string LastName { get; set; }
          public bool IsChecked { get; set; }
        }

1 个答案:

答案 0 :(得分:2)

为lambda语句中的每个成员添加相同的.ToLowerInvariant()调用。

var translatedColl = FriendCollectionTranslated.Where(
                      i => i.FirstName.ToLowerInvariant().StartsWith(text) ||
                           i.LastName.ToLowerInvariant().StartsWith(text) || 
                           i.FirstNameT.ToLowerInvariant().StartsWith(text) || 
                           i.LastNameT.ToLowerInvariant().StartsWith(text)); 

更好的是,使用StringComparison.InvariantCultureIgnoreCase

var translatedColl = FriendCollectionTranslated.Where(
              i => i.FirstName.StartsWith(text,StringComparison.InvariantCultureIgnoreCase) ||
                   i.LastName.StartsWith(text,StringComparison.InvariantCultureIgnoreCase) || 
                   i.FirstNameT.StartsWith(text,StringComparison.InvariantCultureIgnoreCase) || 
                   i.LastNameT.StartsWith(text, StringComparison.InvariantCultureIgnoreCase)); 

然后,您可以从文本变量中删除ToLowerInvariant()调用。