开发者

How do I implement the find on an IList and update object properties on the objects that are found

开发者 https://www.devze.com 2023-01-29 14:14 出处:网络
I am declaring an IList: Dim OrigVendors as IList OrigVendors = new List( of IVendors) I populate the OrigVendors variable and what I want to do is find all the objects that have the ID I am lookin

I am declaring an IList:

Dim OrigVendors as IList
OrigVendors = new List( of IVendors)

I populate the OrigVendors variable and what I want to do is find all the objects that have the ID I am looking for. Then I need to update a property on that object for only the ones found. Is this do able with an IList or do I need to 开发者_运维百科use some other type of collection? I am using VB.net


Assuming that you are making a distinction between the non-Generic version of IList and the Generic List class, then you can filter on specific properties.

Because the IList interface does not derive from the Generic IEnumerable (but rather, the non-Generic version of IEnumerable), you can't use many of the extension methods on the Enumerable class in the System.Linq namespace.

However, if you know that the non-Generic IList implementation contains only implementations of IVendors, then what you can do is call the Cast extension method on IEnumerable like so:

Dim typedVendors = OrigVendors.Cast(of IVendors)

Once you have the typed IEnumerable(of IVendors), you can use any of the LINQ methods you choose to filter out the sequence and perform operations on the items.

0

精彩评论

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

关注公众号