|
|
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#
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 item
The result is below:
'Like' us on our FaceBook page if you find this blog useful. Thanks!
Did you like this post?
|
|
|
||
|
|
|
|
Save on Delicious |
|
|
subscribe via rss |
|
subscribe via e-mail |
|
|
print this post |
|
follow me on twitter |





comments
0 Responses to "Find Distinct Text Using LINQ"Post a Comment