I have a table in a Postgres DB with a string[] column for tags. I now want to get all distinct tags. My first approach was a .SelectMany(d => d.Tags).Distinct()
, wich leads to an error The LINQ expression 'd => d.Tags' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'.
The solution I now came up with is:
var tagsArrays = await _context.MyTable
.Select(d => d.Tags)
.ToListAsync();
var knownTags = tagsArrays
.SelectMany(a => a)
.Distinct()
.ToList();
Obviously this is now done in client side. I know that I could use plain SQL and UNNEST, but nowhere else in the code SQL is used, so I wanted to avoid this.
Is it possible to achieve this with a LINQ style query?