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

关于嵌套Select中使用As不起作用的问题 #1967

Open
zhaoyuehui123 opened this issue Jan 17, 2025 · 0 comments
Open

关于嵌套Select中使用As不起作用的问题 #1967

zhaoyuehui123 opened this issue Jan 17, 2025 · 0 comments

Comments

@zhaoyuehui123
Copy link

问题描述及重现代码:

1、子Select中使用.As("bbb")时生成的sql中不起使用。
2、因为在顶级Select中默认会使用abc...的别名,而此时如果子Select.Where(a=>...)或是在外边定义的Select中使用a起手的话,最终生成的sql就出问题了,如何能够避免在程序这样的问题发生?

var res = await Db.Value.Select<MemberCoiHistoryModel>()
    .Where(x => x.Date >= start && x.Date < end && x.Type == CoiType.New && x.State == CoiState.平稳)
    .GroupBy(x => x.MemberId)
    .WithTempQuery(x => new
    {
        MemberId = x.Key,
        ScanSum = Db.Value.Select<MemberActionDayCountModel>().As("bbb").Where(y => y.MemberId == x.Key && y.Date >= start && y.Date < end).Sum(y => y.ScanCode),
    })
    .ToAggregateAsync(x => new
    {
        MemberCount = x.Count(),
    });

生成的sql如下SELECT COUNT
	( 1 ) as1 
FROM
	(
	SELECT
		a.[MemberId],
		isnull(
			(
			SELECT SUM
				( y.[ScanCode] ) 
			FROM
				[scrm_member_action_day_count] y WITH ( NoLock ) 
			WHERE
				( y.[MemberId] = a.[MemberId] AND y.[Date] >= 20240101 AND y.[Date] < 20240110 ) 
			),
			0 
		) [ScanSum] 
	FROM
		[scrm_member_coi_history] a WITH ( NoLock ) 
	WHERE
		( a.[Date] >= 20240101 AND a.[Date] < 20240110 AND a.[Type] = 1 AND a.[State] = 1 ) 
	GROUP BY
	a.[MemberId] 
	) a

数据库版本

FreeSql.Provider.SqlServer

安装的Nuget包

FreeSql.Provider.SqlServer 3.5.104

.net framework/. net core? 及具体版本

.net 8

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

1 participant