2017-02-22 9 views
20

मैं Winforms एप्लिकेशन पर काम कर रहा हूं। मैं ListView पर एक फ़िल्टर लागू करना चाहता हूँ। किसी फ़ोल्डर में दिए गए नाम के साथ फ़ाइलों को खोजते समय विंडो में सटीक खोज सुविधा को लागू करने की आवश्यकता थी।"प्रासंगिकता मान" द्वारा आदेश

यह पता चला है कि विंडोज Relevance Values का उपयोग फ़ाइलों को खोजने के लिए कर रहा है।

मैं सोच रहा था, शायद Winforms ने इस एल्गोरिदम को एक नियंत्रण या किसी अन्य में लागू किया है? या शायद .NET में कुछ है?

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

आप एक नि: शुल्क पाठ्य की खोज पुस्तकालय की जरूरत है। NuGetting "Lucene.Net" आज़माएं। यहां नमूना कोड का थोड़ा सा है: http://codeclimber.net.nz/archive/2009/09/02/lucenenet-your-first-application/ – Enigmativity

+0

क्या आपने समस्या हल की है? –

उत्तर

3

यहाँ इस लक्ष्य को हासिल करने के लिए एक उदाहरण है: यदि नहीं, तो इस एल्गोरिथ्म है कि मैं मैन्युअल रूप से फ़िल्टर वस्तुओं ऑर्डर करने के लिए उपयोग कर सकते हैं के लिए किसी भी सी # कोड है। इस उदाहरण में फाइल सूची के साथ प्रासंगिकता मूल्यों द्वारा ऑर्डर शामिल है।

कोड:

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

आप खोज और Regex साथ LINQ का उपयोग कर प्रासंगिकता मूल्यों से आदेश कर सकते हैं। नीचे दिए गए कोड का प्रयास करें:

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); 
संबंधित मुद्दे