过滤具有指定值的列表视图 [英] Filtering a listview with a specified value
问题描述
假设这在ListView中显示
A1-desc0-0
A1-desc1-1-
A1-desc2-2
A1-desc1-2-
A2-desc0-0
A2-desc1-1-
A3-desc0-0
A3-desc1-1
我上面有一个清单.我要过滤它,或者只显示具有我想要的指定值的列表.例如,我只想查看具有desc0和desc2值的所有项目,标记A1和A3的页码为0.
因此listview将仅显示:
A1-desc0-0
A3-desc0-0
这样的事情.我该怎么办? (请提供一些代码段)
当我有多个要过滤的指定项目时,我在进行过滤时遇到问题.
Assuiming this is shown in a ListView
A1 - desc0 - 0
A1 - desc1 - 1
A1 - desc2 - 2
A1 - desc1 - 2
A2 - desc0 - 0
A2 - desc1 - 1
A3 - desc0 - 0
A3 - desc1 - 1
I have a list shown above. I want to filter it or just display a list with the specified value that I want. For example I just want to view all items that has a value of desc0 and desc2 with a marker of A1 and A3 with a page number of 0.
so the listview will only show:
A1 - desc0 - 0
A3 - desc0 - 0
Something like that. how should I do it? (kindly provide some code snippet)
I am having problems doing the filtering when i have a multiple specified items to be filtered.
推荐答案
public class SearchList
{
public string Name { get; set; }
public string Desc { get; set; }
public int Page { get; set; }
}
静态void Main()
{
列表< SearchList> searchList = new List< SearchList> {new SearchList {Name ="A1",Desc ="desc0",Page = 0},
新的SearchList {Name ="A1",Desc ="desc1",Page = 1},
新的SearchList {Name ="A1",Desc ="desc2",Page = 2},
新的SearchList {Name ="A1",Desc ="desc1",Page = 2},
新的SearchList {Name ="A2",Desc ="desc0",Page = 0},
新的SearchList {Name ="A2",Desc ="desc1",Page = 1},
新的SearchList {Name ="A3",Desc ="desc0",Page = 0},
新的SearchList {Name ="A3",Desc ="desc1",Page = 1}
};
var query = searchList.Where(a => a.Name =="A1"&& a.Desc =="desc0"&& a.Page == 1);
}
static void Main()
{
List<SearchList> searchList = new List<SearchList> {new SearchList{Name="A1",Desc="desc0",Page=0 },
new SearchList{Name="A1",Desc="desc1",Page=1 },
new SearchList{Name="A1",Desc="desc2",Page=2 },
new SearchList{Name="A1",Desc="desc1",Page=2 },
new SearchList{Name="A2",Desc="desc0",Page=0 },
new SearchList{Name="A2",Desc="desc1",Page=1 },
new SearchList{Name="A3",Desc="desc0",Page=0 },
new SearchList{Name="A3",Desc="desc1",Page=1 }
};
var query = searchList.Where(a => a.Name == "A1" && a.Desc == "desc0" && a.Page == 1);
}
如果您可以将数据源绑定到ListView,则LINQ是提供相同数据的更好方法.例如,ListView中的整个对象可能是字符串或其他对象的集合,则可以使用所需条件编写查询.
listView1.DataSource =用于字符串中的str,其中str.Contains("desc0")或str.Contains("desc2");
语法可能不正确,因为我已经使用VB尝试过此操作
我没有用ListView尝试过这个
If you can bind a data source to your ListView, then LINQ is the better way to provide the same. For example entires in your ListView may be a collections of string or other objects, you can write query with the required condition.
listView1.DataSource = for str in strings where str.Contains( "desc0" ) or str.Contains( "desc2");
syntax may not be proper because I have tried this with VB
I hav''nt tried this with ListView
这篇关于过滤具有指定值的列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!