最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - LINQ way to unnest arrays - Stack Overflow

programmeradmin4浏览0评论

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?

发布评论

评论列表(0)

  1. 暂无评论