2013-03-15 89 views
-1

如何獲得每個列表條目的內容與textQuery.Text進行測試,並且如果它是命中記錄,將被寫入名爲listResx的ListView中的四列中?在LINQ中查詢/搜索列表(在某些情況下)

​​
+0

我很困惑,你問什麼。 textQuery.Text在哪裏?您希望使用哪個查詢操作符(例如,我們在尋找什麼,什麼定義了「命中」)?我們的源數據在哪裏? – theMayer 2013-03-15 21:51:51

+0

也許這可以幫助未來:http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b :-) – 2013-03-15 22:02:09

回答

1
var resultList = MasterList.Where(x => x.id == textQuery.Text || x.en == textQuery.Text || x.fr == textQuery.Text || x.es == textQuery.Text).ToList(); 

這應該給你小,從而匹配列表。你可以從那裏拿走嗎?

0
string keyword = textQuery.Text; 

var hitsQuery = from i in MasterList 
       where i.en == keyword || 
         i.es == keyword || 
         i.fr == keyword || 
         i.id == keyword 
       select i; 

var hits = hitsQuery.ToList(); 

hist將是List<TransResource>。您可以使用它來填充您的ListView,例如使用DataSource屬性:

listResx.DataSource = hits; 
listResx.DataBind(); 
+0

不幸的是,WinForms的ListView沒有DataSource和DataBind ala WebForms。另外,'MasterList'不是數據源,而是'resources.Values' – 2013-03-15 21:52:08

0

試試這個:

var matches = resources.Values.Where(x=>x.id==txtQuery.Text || 
             x.en==txtQuery.Text || 
             x.fr==txtQuery.Text || 
             x.es==txtQuery.Text); 

foreach(var item in matches) 
{ 
    string displayItem = item.id + " " + item.en; 
    listResx.Items.Add(new ListViewItem(displayItem)); 
} 

或用更少的代碼去:

foreach(var item in resources.Values.Where(x=>x.id==txtQuery.Text || 
               x.en==txtQuery.Text || 
               x.fr==txtQuery.Text || 
               x.es==txtQuery.Text)) 
{ 
    string displayItem = item.id + " " + item.en; 
    listResx.Items.Add(new ListViewItem(displayItem)); 
}