-1

如何获取要测试的每个列表条目的内容,textQuery.Text如果它是命中,则将其写入名为 ListView 的四列listResx

List<TransResource> MasterList = new List<TransResource>();

foreach (TransResource x in resources.Values)
{
     MasterList.Add(x);
}
...
public class TransResource
{
    public string id {get; set;}
    public string en {get; set;}
    public string fr {get; set;}
    public string es {get; set;}
}
4

3 回答 3

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

这应该会给你更小的匹配结果列表。你能从那里拿走吗?

于 2013-03-15T21:43:32.623 回答
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();
于 2013-03-15T21:45:37.003 回答
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));
}
于 2013-03-15T21:45:41.743 回答