[英]Filter ObservableCollection by value from Comma seperated string
I am trying to filter an existing ObservableCollection
by looking at one of its properties that holds a comma separated string, which I want to match against a string value and create a new ObservableCollection with matching results from this. 我正在尝试通过查看包含以逗号分隔的字符串的属性之一来过滤现有的
ObservableCollection
,我希望将其与字符串值进行匹配,并创建一个新的ObservableCollection,并从中获得匹配的结果。
I have the following code at the moment but am a little stuck on how to use lambda correctly to do so 我目前有以下代码,但对如何正确使用lambda有点坚持
string s = "some string";
ObservableCollection<MyCollection> oc = myDataservice.ExistingObservableCollection.FindAll(x => x.KommaSeperatedString.Contains(s));
Not sure how to go about getting the desired result 不确定如何取得理想的结果
As result i would like a new ObservableCollection with all objects that have a matching string in the ExistingObservableCollections comma separated string. 结果,我想要一个新的ObservableCollection,其中在ExistingObservableCollections逗号分隔的字符串中具有匹配字符串的所有对象。
Any help is appreciated as always. 一如既往地提供任何帮助。
var list = oc.All(s=> s.KommaSeperatedString.Contains(s)).ToList<MyCollection>();
如果要构造另一个ObservableCollection
:
var observableCollection = new ObservableCollection<MyCollection>(myList);
You are comparing against a string, you should try to convert it to a list first. 您正在与字符串进行比较,您应该首先尝试将其转换为列表。 Lists are far more flexible for this kind of filtering.
列表对于这种过滤更加灵活。
s.Split(',').ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.