Only when we start to write code in vb.net, we will realize some pactical scenarios we should have preferred C#.net as programming language. One such case happened to me, when i was trying to write something in LINQ along with vb.net. But after a lot of research and search, i achieved my goal with vb.net itself. Sharing are those below:

To remove an item from Ilist collection using LINQ and vb.net

Dim validLineItems As IList = ‘Get the collection of data from db or any source as Ilist
Dim itemToRemove As Object = (From p In validLineItems Where p.UNIQUEID = CType(“Unique ID in hand”, Integer)) .SingleOrDefault()
validLineItems.Remove(itemToRemove)

Now the same ‘validLineItems’ contains the collection after teh removed item, which you can bind straightaway to any databinding control (dropdown, grid etc)

To remove an item from XML string, using LINQ and vb.net

Dim xd As XDocument = XDocument.Parse(xmlInputSource) ‘xmlInputSource is a well-formatted xml
Dim itemsToRemove = (From element In xd.Root.Elements(“PARENTNODE”) Where element.Attribute(“CHILDNODE”).Value = ‘value in hand’)
itemsToRemove.Remove()

xmlInputSource, PARENTNODE, CHILDNODE are values you have to replace

2 thoughts on “LINQ samples to be used with vb.net”

Leave a Reply