2017-02-22 20 views
20

Tôi đang làm việc trên ứng dụng winforms. Tôi muốn áp dụng một bộ lọc trên ListView. Yêu cầu là triển khai tính năng tìm kiếm chính xác trong cửa sổ khi tìm kiếm tệp có tên đã cho trong thư mục.Đặt hàng theo "Giá trị phù hợp"

Hóa ra Windows đang sử dụng Relevance Values để đặt hàng các tệp đã tìm thấy.

Tôi đã suy nghĩ, có thể winforms đã triển khai thuật toán này trong một điều khiển này hay cách khác? Hoặc có lẽ NET có một số nơi? Nếu không, là có bất kỳ mã # C cho thuật toán này mà tôi có thể sử dụng tự đặt đối tượng lọc:

var searchFor = "search"; 
var newList = oldList.Select(x =>x.Contains(searchFor)) 
        .OrderBy(x => RelevanceValues(x,searchFor)) 
        .ToList(); 
+1

http://stackoverflow.com/questions/19272920/enumerating-files-of-specific-type-in-windows –

+0

Bạn cần một thư viện tìm kiếm văn bản miễn phí. Hãy thử NuGetting "Lucene.Net". Đây là một chút mẫu mã: http://codeclimber.net.nz/archive/2009/09/02/lucenenet-your-first-application/ – Enigmativity

+0

Bạn đã giải quyết được vấn đề chưa? –

Trả lời

3

Dưới đây là một ví dụ để đạt được điều này. Ví dụ này chứa Order By Relevance Values ​​với danh sách File.

Mã sản phẩm:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Text.RegularExpressions; 
using System.Threading.Tasks; 
using System.Windows.Forms;  

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     // textBox1 for search string 
     private System.Windows.Forms.TextBox textBox1; 
     // listView1 for show result 
     private System.Windows.Forms.ListView listView1; 
     private System.Windows.Forms.Button button1; 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     class MyListViewItem : ListViewItem 
     { 
      public int Index { get; set; } 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      List<MyListViewItem> myList = new List<MyListViewItem>(); 

      // open folder browser to get folder path  
      FolderBrowserDialog result = new FolderBrowserDialog(); 
      if (result.ShowDialog() == DialogResult.OK) 
      { 
       // get all file list 
       string[] files = Directory.GetFiles(result.SelectedPath); 
       foreach (string item in files) 
       { 
        // find the relevance value based on search string 
        int count = Regex.Matches(Regex.Escape(item.ToLower()), textBox1.Text.ToLower()).Count; 
        myList.Add(new MyListViewItem() { Text = item, Index = count }); 
       } 
      } 

      List<ListViewItem> list = new List<ListViewItem>(); 
      // add file name in final list with order by relevance value 
      foreach (var item in myList.OrderByDescending(m => m.Index).ToList()) 
      { 
       list.Add(new ListViewItem() { Text = item.Text }); 
      } 

      listView1.Items.AddRange(list.ToArray()); 
     } 
    } 
} 
2

Bạn có thể tìm kiếm và trật tự của các giá trị phù hợp sử dụng LINQ với Regex. Vui lòng thử mã dưới đây:

var searchFor = "search";  
var newList = oldList.Select(l => new 
     { 
      SearchResult = l, 
      RelevanceValue = (Regex.Matches(Regex.Escape(l.Text.ToLower()), searchFor.ToLower()).Count) 
     }) 
      .OrderByDescending(r => r.RelevanceValue) 
      .Select(r => r.SearchResult); 
Các vấn đề liên quan