As a DBA I sometimes have to do some programming or scripting. One of the things that I find very useful is knowing how to remove duplicate items from an array. Most recently I had a C# application I had built that was generating duplicate items (they weren't really duplicates the part numbers were the same, however the revision was different. But the fact that the rev was different was irrelevant to the people consuming the data. Now one of the fields I needed to select was of the text data type, so I could not include DISTINCT in the query's select clause. I had to remove the duplicates from the array holding the data set. The .NET Framework 3.5 had made this a snap. You used to have to loop through the array and perform a comparison, but now you can do it with just a single line of code!
PowerShell
$abc = @("a", "a", "b", "c", "d", "e", "d") $abc = $abc | Get-Unique |
C#
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace UniqueArray_example { class Program { static void Main(string[] args) { string[] abc = {"a", "a", "b", "c", "d", "a", "c", "e"}; abc = abc.Distinct().ToArray(); //That's it. One line. foreach (string letter in abc) { Console.WriteLine(letter); } } } } |
0 comments:
Post a Comment