<p>I have a List of Foo.</p>

<p>Foo has a string property named Bar.</p>

<p>I'd like to use <strong>LINQ</strong> to get a string[] of <strong>distinct</strong> values for Foo.Bar in List of Foo.</p>

<p>How can I do this?</p>

[{'answer_id': 40477, 'author': 'Lasse V. Karlsen', 'author_id': 267, 'author_profile': 'https://Stackoverflow.com/users/267', 'pm_score': 2, 'selected': False, 'text': '<p>Try this:</p>\n\n<pre><code>var distinctFooBars = (from foo in foos\n                       select foo.Bar).Distinct().ToArray();\n</code></pre>\n'}, {'answer_id': 40488, 'author': 'Guy', 'author_id': 1463, 'author_profile': 'https://Stackoverflow.com/users/1463', 'pm_score': 2, 'selected': False, 'text': '<p>This should work if you want to use the fluent pattern:</p>\n\n<pre><code>string[] arrayStrings = fooList.Select(a =&gt; a.Bar).Distinct().ToArray();\n</code></pre>\n'}, {'answer_id': 40497, 'author': 'chakrit', 'author_id': 3055, 'author_profile': 'https://Stackoverflow.com/users/3055', 'pm_score': 4, 'selected': True, 'text': '<p>I\'d go lambdas... wayyy nicer</p>\n\n<pre><code>var bars = Foos.Select(f =&gt; f.Bar).Distinct().ToArray();\n</code></pre>\n\n<p>works the same as what @lassevk posted.</p>\n\n<p>I\'d also add that you might want to keep from converting to an array until the last minute. </p>\n\n<p>LINQ does some optimizations behind the scenes, queries stay in its query form until explicitly needed. So you might want to build everything you need into the query first so any possible optimization is applied altogether.</p>\n\n<p>By evaluation I means asking for something that explicitly requires evalution like "Count()" or "ToArray()" etc.</p>\n'}, {'answer_id': 40498, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>Shouldn't you be able to do something like:</p>\n\n<pre><code>var strings = (from a in fooList select a.Bar).Distinct();\nstring[] array = strings.ToArray();\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/40465', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1946/']