Uniq in C #

Is there an easy way to get only unique values ​​from a list of strings in C #? Today my google-fu is failing.

(I know that I can put them in a different structure and pull them out again. I'm looking stupidly easy like the Ruby.uniq method. C # has a hell of a lot of the rest, so I'm probably just using the synonym incorrectly.)

In particular, this comes from Linq, so if Linq has a built-in way to select only unique rows, it will be even colder.

+3
source share
2 answers
List<string> strings = new string[] { "Hello", "Hello", "World" }.ToList();

strings = strings.Distinct().ToList();
+9
source

In .net 3.5: -

var strings = new List<string> { "one", "two", "two", "three" };
var distinctStrings = strings.Distinct(); // IEnumerable<string>
var listDistinctStrings = distinctStrings.ToList(); // List<string>

The boom of shaka-varnish!

+6
source

Source: https://habr.com/ru/post/1703467/


All Articles