开发者

Convert tricky linq expression to SQL

开发者 https://www.devze.com 2023-02-11 01:49 出处:网络
I have following linq expression: var files = posts .Select(post => int.Parse(post.Length.ToString()[0].ToString()))

I have following linq expression:

        var files = posts
            .Select(post => int.Parse(post.Length.ToString()[0].ToString()))
            .GroupBy(c => c)
            .ToDictionary(g => g.Key, g => g.Count());

I want to translate it to SQL (Microsoft SQL). Where source is IEnumerable<string>. The problem is how to group records by it's first digit and count quantity of items in each group.

The database has one table Posts, that has column Body(varchar(max)). I'm interested in first digit of it's length.

Thanks!开发者_C百科


SELECT firstDigit as Key, COUNT(*) as theCount
FROM
(
  SELECT convert(int, LEFT(convert(varchar(30), f), 1)) as firstDigit
  FROM Source as f
) as sub
GROUP BY firstDigit
0

精彩评论

暂无评论...
验证码 换一张
取 消