Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

是否可以考虑增加对 string.Join() 方法的支持 #405

Closed
hd2y opened this issue Aug 6, 2020 · 10 comments
Closed

是否可以考虑增加对 string.Join() 方法的支持 #405

hd2y opened this issue Aug 6, 2020 · 10 comments

Comments

@hd2y
Copy link
Contributor

hd2y commented Aug 6, 2020

代码:

var list = freeSql.Queryable<Music>()
    .ToList((a) => new
    {
        a.Id,
        a.Name,
        Tags = string.Join(",", freeSql.Select<Tag>().Where(b => a.Id == b.MusicId).ToList(b => b.Name))
    });

public class Music
{
    [Column(IsIdentity = true)]
    public int Id { get; set; }

    public string Name { get; set; }
}

public class Tag
{
    [Column(IsIdentity = true)]
    public int Id { get; set; }

    public int MusicId { get; set; }

    public string Name { get; set; }
}

SQL 脚本:

-- SQLite
SELECT a.Id, a.Name, (SELECT GROUP_CONCAT(b.Name) FROM Tag b where a.Id = b.MusicId) FROM Music a;

其他数据库的实现:

@2881099
Copy link
Collaborator

2881099 commented Aug 6, 2020

在考虑,何不直接在子查询里面处理,这样可以用在更多地方。

用表达式解析处理:

    .ToList((a) => new
    {
        a.Id,
        a.Name,
        Tags = freeSql.Select<Tag>().Where(b => a.Id == b.MusicId)
            .First(b => SqlExt.GroupConcat(b.Name).Separator(",").ToValue())
    });

MySql 目前可以这样用,SqlExt.GroupConcat 已经实现了

@hd2y
Copy link
Contributor Author

hd2y commented Aug 7, 2020

在考虑,何不直接在子查询里面处理,这样可以用在更多地方。

用表达式解析处理:

    .ToList((a) => new
    {
        a.Id,
        a.Name,
        Tags = freeSql.Select<Tag>().Where(b => a.Id == b.MusicId)
            .First(b => SqlExt.GroupConcat(b.Name).Separator(",").ToValue())
    });

MySql 目前可以这样用,SqlExt.GroupConcat 已经实现了

其实主要在 SQL Server 中使用,目前类似的查询都还是用 SQL 来写,上面只是临时用 SQLite 写了个例子。🙂

@2881099
Copy link
Collaborator

2881099 commented Aug 7, 2020

sqlserver 太蛋疼,"中国,上海,深圳," 最后的逗号很难处理。

@2881099
Copy link
Collaborator

2881099 commented Aug 7, 2020

暂时只针对性的对 sqlserver 进行 string.Join + ToList 解析

fsql.Select<StringJoin01>().ToList(a => 
    string.Join(",", fsql.Select<StringJoin01>().As("b").ToList(b => b.name))
);

@hd2y
Copy link
Contributor Author

hd2y commented Aug 8, 2020

sqlserver 太蛋疼,"中国,上海,深圳," 最后的逗号很难处理。

这个忘记说了,前面引用的那个文章其实用法有问题,实际应该是把逗号放在前面,然后用 stuff 函数去处理。😂

可以参考一下:https://www.cnblogs.com/stevenjson/p/3673239.html

@2881099
Copy link
Collaborator

2881099 commented Aug 8, 2020

sqlserver 太蛋疼,"中国,上海,深圳," 最后的逗号很难处理。

这个忘记说了,前面引用的那个文章其实用法有问题,实际应该是把逗号放在前面,然后用 stuff 函数去处理。😂

可以参考一下:https://www.cnblogs.com/stevenjson/p/3673239.html

不知道有这个函数,前后都试过,带会看能不能把 string.Join 一统江湖

@2881099
Copy link
Collaborator

2881099 commented Aug 8, 2020

SELECT listagg(b."NAME",',') within group
FROM "STRINGJOIN01" b

还非要在 within group 后面加 order by,有没有其他办法解决这个?

2881099 pushed a commit that referenced this issue Aug 8, 2020
@2881099
Copy link
Collaborator

2881099 commented Aug 8, 2020

SELECT listagg(b."NAME",',') within group
FROM "STRINGJOIN01" b

还非要在 within group 后面加 order by,有没有其他办法解决这个?

用了一个复杂逻辑解决的,order by 延用子查询内的 .OrderBy() 或者 order by pk 或者 order by 1

@2881099
Copy link
Collaborator

2881099 commented Aug 8, 2020

适配完成了,也测试了,老板验收

2881099 pushed a commit that referenced this issue Aug 8, 2020
@hd2y
Copy link
Contributor Author

hd2y commented Aug 8, 2020

验收不敢当,这效率只能说叶老板大气。👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants