开发者

How to select specific item from BindingList<KeyValuePair<string, string>>?

开发者 https://www.devze.com 2023-03-13 20:25 出处:网络
BindingList<KeyValuePair<string, string>> properties = new BindingList<KeyValuePair<string, string>>();
BindingList<KeyValuePair<string, string>> properties = new BindingList<KeyValuePair<string, string>>();

Code above stores 开发者_C百科around 10-30 objects as KeyValuePair<string, string>

I need to somehow select an element let's say with key "id"

How do I go about that?


properties.Select(k => k.Key == "id").FirstOrDefault();


BindingList does not directly implement IEnumerable so FirstOrDefault() (LINQ to objects) will not work, even when using System.Linq. You need to target the underlying collection. The following worked for me:

var myObject = ( (IEnumerable<SomeObjectType>) myBindingSource.List ).FirstOrDefault( d => d.SomeProperty == "some property value" );
0

精彩评论

暂无评论...
验证码 换一张
取 消