Skip to content
Yasser Moradi edited this page Aug 26, 2015 · 2 revisions

Description:

This method is used to generate a list of unique items from a single source, by filtering out any duplicate items. The sample code below finds all of the distinct values from the first example array. Note that the duplicated D has been removed but the two A's are still present because one is lower case and the other is capitalized.

Sample:

let set1 = ['A', 'a', 'B', 'C', 'D', 'D', 'E'];
let distinct1 = set1.asEnumerable().distinct().toArray();

console.log('Distinct method remove duplicated items ...');

for (var index = 0; index < distinct1.length; index++)
  console.log(distinct1[index]);

More Description:

The above sample uses the default comparer for the data type being processed. You can use an alternative comparer by providing it as a second parameter. Below the comparison is case-insensitive and the lower case letter A has been removed from the results accordingly.

Sample:

let distinct2 = set1.asEnumerable().distinct((a, b) => a.toLowerCase() === b.toLowerCase())
  .toArray();

console.log('Distinct method remove duplicated items using comparer ...');
for (var index = 0; index < distinct2.length; index++)
  console.log(distinct2[index]);
Clone this wiki locally