mirror of
				https://github.com/nsnail/FreeSql.git
				synced 2025-11-04 17:20:49 +08:00 
			
		
		
		
	- 修复 GroupBy(..).Count() 开启参数化无效的 bug #390; UseGenerateCommandParameterWithLambda
This commit is contained in:
		
							
								
								
									
										60
									
								
								FreeSql.Tests/FreeSql.Tests/Issues/390.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								FreeSql.Tests/FreeSql.Tests/Issues/390.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,60 @@
 | 
			
		||||
using FreeSql.DataAnnotations;
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Diagnostics;
 | 
			
		||||
using System.Text;
 | 
			
		||||
using System.Threading;
 | 
			
		||||
using Xunit;
 | 
			
		||||
 | 
			
		||||
namespace FreeSql.Tests.Issues
 | 
			
		||||
{
 | 
			
		||||
    public class _390
 | 
			
		||||
    {
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void SelectTest()
 | 
			
		||||
        {
 | 
			
		||||
            IFreeSql db = new FreeSql.FreeSqlBuilder()
 | 
			
		||||
                        .UseConnectionString(FreeSql.DataType.Oracle, "user id=1user;password=123456;data source=//127.0.0.1:1521/XE;Pooling=true;Max Pool Size=1")
 | 
			
		||||
                       .UseNameConvert(FreeSql.Internal.NameConvertType.ToUpper)
 | 
			
		||||
                       .UseGenerateCommandParameterWithLambda(true)
 | 
			
		||||
                       .UseAutoSyncStructure(true)
 | 
			
		||||
                        .UseMonitorCommand(cmd => Trace.WriteLine("\r\n线程" + Thread.CurrentThread.ManagedThreadId + ": " + cmd.CommandText))
 | 
			
		||||
                        .Build();
 | 
			
		||||
 | 
			
		||||
            var startTime = DateTime.Now;
 | 
			
		||||
            var endTime = DateTime.Now;
 | 
			
		||||
 | 
			
		||||
            var cou = db.Select<V_HospitalReport>()
 | 
			
		||||
                .Where(a => a.ScheduledDttm.Date >= startTime.Date && a.ScheduledDttm.Date <= (endTime.AddDays(1)).Date)
 | 
			
		||||
                .GroupBy(a =>
 | 
			
		||||
                new
 | 
			
		||||
                {
 | 
			
		||||
                    a.HospitalName,
 | 
			
		||||
                    a.Dep,
 | 
			
		||||
                    a.Instrna,
 | 
			
		||||
                    a.ConfirmDoctorName,
 | 
			
		||||
                    a.ScheduledDttm.Date
 | 
			
		||||
                })
 | 
			
		||||
                .Count();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Table(Name = "V_HospitalReport")]
 | 
			
		||||
        public class V_HospitalReport
 | 
			
		||||
        {
 | 
			
		||||
            [Column(Name = "hospital_name")]
 | 
			
		||||
            public string HospitalName { get; set; }
 | 
			
		||||
 | 
			
		||||
            [Column(Name = "dep")]
 | 
			
		||||
            public string Dep { get; set; }
 | 
			
		||||
 | 
			
		||||
            [Column(Name = "instrna")]
 | 
			
		||||
            public string Instrna { get; set; }
 | 
			
		||||
 | 
			
		||||
            [Column(Name = "confirm_doctor_name")]
 | 
			
		||||
            public string ConfirmDoctorName { get; set; }
 | 
			
		||||
 | 
			
		||||
            [Column(Name = "Scheduled_Dttm")]
 | 
			
		||||
            public DateTime ScheduledDttm { get; set; }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -2,6 +2,7 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Data;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Linq.Expressions;
 | 
			
		||||
using System.Reflection;
 | 
			
		||||
@@ -13,13 +14,13 @@ namespace FreeSql.Internal.CommonProvider
 | 
			
		||||
    public class SelectGroupingProvider
 | 
			
		||||
    {
 | 
			
		||||
        public IFreeSql _orm;
 | 
			
		||||
        public object _select;
 | 
			
		||||
        public Select0Provider _select;
 | 
			
		||||
        public ReadAnonymousTypeInfo _map;
 | 
			
		||||
        public string _field;
 | 
			
		||||
        public CommonExpression _comonExp;
 | 
			
		||||
        public List<SelectTableInfo> _tables;
 | 
			
		||||
 | 
			
		||||
        public SelectGroupingProvider(IFreeSql orm, object select, ReadAnonymousTypeInfo map, string field, CommonExpression comonExp, List<SelectTableInfo> tables)
 | 
			
		||||
        public SelectGroupingProvider(IFreeSql orm, Select0Provider select, ReadAnonymousTypeInfo map, string field, CommonExpression comonExp, List<SelectTableInfo> tables)
 | 
			
		||||
        {
 | 
			
		||||
            _orm = orm;
 | 
			
		||||
            _select = select;
 | 
			
		||||
@@ -139,7 +140,7 @@ namespace FreeSql.Internal.CommonProvider
 | 
			
		||||
 | 
			
		||||
    public class SelectGroupingProvider<TKey, TValue> : SelectGroupingProvider, ISelectGrouping<TKey, TValue>
 | 
			
		||||
    {
 | 
			
		||||
        public SelectGroupingProvider(IFreeSql orm, object select, ReadAnonymousTypeInfo map, string field, CommonExpression comonExp, List<SelectTableInfo> tables)
 | 
			
		||||
        public SelectGroupingProvider(IFreeSql orm, Select0Provider select, ReadAnonymousTypeInfo map, string field, CommonExpression comonExp, List<SelectTableInfo> tables)
 | 
			
		||||
            :base(orm, select, map, field, comonExp, tables) { }
 | 
			
		||||
 | 
			
		||||
        public string ToSql<TReturn>(Expression<Func<ISelectGroupingAggregate<TKey, TValue>, TReturn>> select, FieldAliasOptions fieldAlias = FieldAliasOptions.AsIndex) => InternalToSql(select, fieldAlias);
 | 
			
		||||
@@ -154,26 +155,24 @@ namespace FreeSql.Internal.CommonProvider
 | 
			
		||||
 | 
			
		||||
        public ISelectGrouping<TKey, TValue> Skip(int offset)
 | 
			
		||||
        {
 | 
			
		||||
            var method = _select.GetType().GetMethod("Skip", new[] { typeof(int) });
 | 
			
		||||
            method.Invoke(_select, new object[] { offset });
 | 
			
		||||
            _select._skip = offset;
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
        public ISelectGrouping<TKey, TValue> Offset(int offset) => this.Skip(offset);
 | 
			
		||||
        public ISelectGrouping<TKey, TValue> Limit(int limit)
 | 
			
		||||
        {
 | 
			
		||||
            var method = _select.GetType().GetMethod("Limit", new[] { typeof(int) });
 | 
			
		||||
            method.Invoke(_select, new object[] { limit });
 | 
			
		||||
            _select._limit = limit;
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
        public ISelectGrouping<TKey, TValue> Take(int limit) => this.Limit(limit);
 | 
			
		||||
        public ISelectGrouping<TKey, TValue> Page(int pageNumber, int pageSize)
 | 
			
		||||
        {
 | 
			
		||||
            var method = _select.GetType().GetMethod("Page", new[] { typeof(int), typeof(int) });
 | 
			
		||||
            method.Invoke(_select, new object[] { pageNumber, pageSize });
 | 
			
		||||
            _select._skip = Math.Max(0, pageNumber - 1) * pageSize;
 | 
			
		||||
            _select._limit = pageSize;
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public long Count() => long.TryParse(string.Concat(_orm.Ado.ExecuteScalar($"select count(1) from ({this.ToSql($"1{_comonExp._common.FieldAsAlias("as1")}")}) fta")), out var trylng) ? trylng : default(long);
 | 
			
		||||
        public long Count() => long.TryParse(string.Concat(_orm.Ado.ExecuteScalar(CommandType.Text, $"select count(1) from ({this.ToSql($"1{_comonExp._common.FieldAsAlias("as1")}")}) fta", _select._params.ToArray())), out var trylng) ? trylng : default(long);
 | 
			
		||||
        public ISelectGrouping<TKey, TValue> Count(out long count)
 | 
			
		||||
        {
 | 
			
		||||
            count = this.Count();
 | 
			
		||||
@@ -202,7 +201,7 @@ namespace FreeSql.Internal.CommonProvider
 | 
			
		||||
 | 
			
		||||
#if net40
 | 
			
		||||
#else
 | 
			
		||||
        async public Task<long> CountAsync() => long.TryParse(string.Concat(await _orm.Ado.ExecuteScalarAsync($"select count(1) from ({this.ToSql($"1{_comonExp._common.FieldAsAlias("as1")}")}) fta")), out var trylng) ? trylng : default(long);
 | 
			
		||||
        async public Task<long> CountAsync() => long.TryParse(string.Concat(await _orm.Ado.ExecuteScalarAsync(CommandType.Text, $"select count(1) from ({this.ToSql($"1{_comonExp._common.FieldAsAlias("as1")}")}) fta", _select._params.ToArray())), out var trylng) ? trylng : default(long);
 | 
			
		||||
 | 
			
		||||
        public Task<List<TReturn>> ToListAsync<TReturn>(Expression<Func<ISelectGroupingAggregate<TKey, TValue>, TReturn>> select) => InternalToList(select, typeof(TReturn), true) as Task<List<TReturn>>;
 | 
			
		||||
        async public Task<Dictionary<TKey, TElement>> ToDictionaryAsync<TElement>(Expression<Func<ISelectGroupingAggregate<TKey, TValue>, TElement>> elementSelector)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user