Wednesday, October 28, 2015

How to get distinct records from array?

Using linq, it's pretty easy to get distinct records from array.
Lets say you have file filled with record and you read the file into array.
Now on this array, you can use lambda/linq expression to filter the list.
The code will keep the first record it found and throws all duplicates after it found the first one.

For my work, it was matter because I need to process in the order
they receive.


' Read complete file.
Dim lines() As String = File.ReadAllLines("C\dummyrecord.txt")


' group array items and get the first record.
Dim distinctList = lines.GroupBy(Function(x) x.ToString).Select(Function(grp) grp.First).ToArray


That's it!!


This line will return an String Array as well. Depending on your choice, you can also get
different data structures instead of array.


Linq Explanation:

2 functions are used: GroupBy and Select. Both take function as an argument which let you reference the items of object (array/list).
GroupBy will take function as argument using which you can reference elements of array/object.
Then you using Select method, you reference the grouped items and get the first record of many duplicate records.


HTH!

No comments: