Another good use for LINQ popped up today in the forums. The person
asked how to get distinct values from a series of text. I once again
said LINQ! Here’s the sample text:
Apples, Oranges, Apples, Melons
We can use the Split method to turn this into an array. We can then use the Distinct method to only return the distinct values. Here’s the code:
C#

Apples, Oranges, Apples, Melons
We can use the Split method to turn this into an array. We can then use the Distinct method to only return the distinct values. Here’s the code:
C#
var unique = "Apples, Oranges, Apples, Melons" .Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries).Distinct(); foreach (var item in unique) { Response.Write(item + "<br />"); }VB.NET
Dim unique = "Apples, Oranges, Apples, Melons" _ .Split(New String(){", "},StringSplitOptions.RemoveEmptyEntries).Distinct() For Each item In unique Response.Write(item & "<br />") Next itemThe result is below:
No comments:
Post a Comment