public async Task <bool> ContainsAsync(string keyword = "") { return(await Task <bool> .Factory.StartNew(() => { lock (_lockerFilter) { _filterlist?.Clear(); if (string.IsNullOrEmpty(keyword)) { _filterlist = new List <VirtualListItem>(_list.Select(x => x.Value)); return true; } _filterlist = new List <VirtualListItem>(); foreach (SearchStringList.StringPosition pos in _indexSearchStringList.FindAll(keyword)) { var tempUser = _list[pos.ListIndex]; var str = tempUser.ToString(); var left = str.Substring(0, pos.StringIndex); var sub = str.Substring(pos.StringIndex, keyword.Length); var right = str.Substring(pos.StringIndex + keyword.Length); tempUser.UpdateColorText(left, sub, right); _filterlist.Add(tempUser); } return _filterlist.Count > 0; } })); }
// little test console app static void Main(string[] args) { var list = new SearchStringList(true); int count = 20; int idx = 0; // for (int i = 0; i < count; i++) // { // for (int j = 0; j < count; j++) // { // for (int z = 0; z < count; z++) // { // list.Add(i.ToString() + j.ToString() + z.ToString()); // } // } // } list.Add("12996"); // var list = new SearchStringList(true); // list.Add("Now is the time"); // list.Add("for all good men"); // list.Add("Time now for something"); // list.Add("something completely different"); string keyword = "12"; foreach (var pos in list.FindAll(keyword)) { Console.WriteLine(pos.ToString() + " =>" + list[pos.ListIndex] + " pos.StringIndex:" + pos.StringIndex); var str = list[pos.ListIndex]; var left = str.Substring(0, pos.StringIndex); var sub = str.Substring(pos.StringIndex, keyword.Length); var right = str.Substring(pos.StringIndex + keyword.Length); Console.WriteLine("left:" + left + " sub:" + sub + " right:" + right); } Console.ReadLine(); // while (true) // { // string keyword = Console.ReadLine(); // if (keyword.Length == 0) break; // foreach (var pos in list.FindAll(keyword)) // { // Console.WriteLine(pos.ToString() + " =>" + list[pos.ListIndex]); // } // } }
private void Button_Click(object sender, RoutedEventArgs e) { SqlConnection con = new SqlConnection(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"); con.Open(); string query = "SELECT Name, SalesRate FROM Items"; sda = new SqlDataAdapter(query, con); sda.SelectCommand.ExecuteNonQuery(); tblData = new DataTable(); sda.Fill(tblData); var list = new SearchStringList(true); var list2 = new SearchStringList(false); foreach (DataRow theRow in tblData.Rows) { list.Add(theRow.ItemArray[0].ToString()); list2.Add(theRow.ItemArray[1].ToString()); } string keyword = SearchText.Text; if (keyword.Length == 0) { DisplaySearch.ItemsSource = tblData.DefaultView; } else { DataTable tblData2 = new DataTable(); tblData2.Columns.Add(new DataColumn() { ColumnName = "Name", DataType = System.Type.GetType("System.String"), AllowDBNull = true, DefaultValue = String.Empty, MaxLength = 100 }); tblData2.Columns.Add(new DataColumn() { ColumnName = "Price", DataType = System.Type.GetType("System.String"), AllowDBNull = true, DefaultValue = String.Empty, MaxLength = 100 }); foreach (var pos in list.FindAll(keyword)) { Console.WriteLine(pos.ToString() + " =>" + list[pos.ListIndex]); DataRow dr = tblData2.NewRow(); dr[0] = list[pos.ListIndex]; dr[1] = list2[pos.ListIndex]; tblData2.Rows.Add(dr); } DisplaySearch.ItemsSource = tblData2.DefaultView; } }