How to get an array of different property values ​​from memory lists?

I have a list of foo.

Foo has a string property called Bar.

I would like to use LINQ to get the string [] from various values ​​for Foo.Bar in the Foo list.

How can i do this?

+3
source share
4 answers

I would go lambda ... better ...

var bars = Foos.Select(f => f.Bar).Distinct().ToArray();

works the same as published by @lassevk.

I would also add that you can refuse to convert to an array until the last minute.

LINQ , , . , , , , .

-, , "Count()" "ToArray()" ..

+4

, :

string[] arrayStrings = fooList.Select(a => a.Bar).Distinct().ToArray();
+3

Try the following:

var distinctFooBars = (from foo in foos
                       select foo.Bar).Distinct().ToArray();
+2
source

Can't you do something like this?

var strings = (from a to fooList, select a.Bar) .Distinct (); string [] array = strings.ToArray ();

0
source

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


All Articles