gbase8s按照时间分组报错 返回

SqlSugar 沟通中
15 448
该叫什么 moozy 发布于1个月前
悬赏:0 飞吻
using SqlSugar;

namespace SqlSugarTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Create db SqlSugar.GBaseCore 5.1.4.216  SqlSugarCore 5.1.4.210
            Console.WriteLine("#### SqlSugarClient Start ####");
            SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                DbType = DbType.GBase,
                ConnectionString = "Host=192.168.1.11;Service=9088;Server=gbase01;Database=testdb;Protocol=onsoctcp;Uid=gbasedbt;Pwd=GBase@123;Db_locale=zh_CN.utf8;Client_locale=zh_CN.utf8",
                InitKeyType = InitKeyType.Attribute,
                IsAutoCloseConnection = true,
                AopEvents = new AopEvents
                {
                    OnLogExecuting = (sql, p) =>
                    {
                        Console.WriteLine(sql);
                        Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
                    }
                }
            });

            //If no exist create datebase ,会报错手动建库
            //db.DbMaintenance.CreateDatabase();

            //Create tables
            db.CodeFirst.InitTables(typeof(OrderItem),typeof(Order));

            //Init Data
            for (int i = 0; i < 10; i++)
            {
                var id = db.Insertable(new Order() { Name = "order1", CustomId = 1, Price = 0, CreateTime = DateTime.Now }).ExecuteReturnIdentity();

                //Insert data
                db.Insertable(new OrderItem() { OrderId = id, Price = 0, CreateTime = DateTime.Now }).ExecuteCommand();
            }

            //方式1.此处报错,pg等数据库支持
            db.Queryable<Order>()
                .GroupBy(s => s.CreateTime.ToString("yyyy-MM-dd"))
                .Select(s => new
                {
                    date = s.CreateTime.ToString("yyyy-MM-dd"),
                    count = SqlFunc.AggregateCount(s.Id)
                })
                .ToList();

             //方式2.此处报错,pg等数据库支持
            db.Queryable<Order>()
                .GroupBy(s => $"{SqlFunc.DateValue(s.CreateTime, DateType.Year)}")
                .Select(s => new
                {
                    date = $"{SqlFunc.DateValue(s.CreateTime, DateType.Year)}",
                    count = SqlFunc.AggregateCount(s.Id)
                })
                .ToList();
        }
    }

    [SqlSugar.SugarTable("OrderDetail")]
    public class OrderItem
    {
        [SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
        public int ItemId { get; set; }
        public int OrderId { get; set; }
        public decimal? Price { get; set; }
        [SqlSugar.SugarColumn(IsNullable = true)]
        public DateTime? CreateTime { get; set; }
    }

    public class Order
    {
        [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
        public int Id { get; set; }

        public string Name { get; set; }
        public decimal Price { get; set; }
        [SugarColumn(IsNullable = true)]
        public DateTime CreateTime { get; set; }
        [SugarColumn(IsNullable = true)]
        public int CustomId { get; set; }
        [SugarColumn(IsIgnore = true)]
        public List<OrderItem> Items { get; set; }
    }
}


热忱回答15

  • fate sta fate sta VIP0
    1个月前

        .GroupBy(s => $"{SqlFunc.DateValue(s.CreateTime, DateType.Year)}")

    改成

    GroupBy(s=>s.CreateTime.Year)

    0 回复
  • moozy moozy VIP0
    1个月前

    1.改成你说的这种实现方式也会报错,以下是我用orm生成的sql在g8s的客户端执行了一些,提示报错。

    image.png

    2.另外,我需要的分组也更详细些,这个枚举里面的按时间分组我都需要,这些在pg数据库里面是没有问题的。

    image.png

                    var bagQuarterData = bagQuery.GroupBy(s => new

                    {

                        DateString = $"{SqlFunc.DateValue(s.CreateTime, DateType.Year)}/{SqlFunc.DateValue(s.CreateTime, DateType.Quarter)}"

                    })

                        .Select(s => new

                        {

                            DateString = $"{SqlFunc.DateValue(s.CreateTime, DateType.Year)}/{SqlFunc.DateValue(s.CreateTime, DateType.Quarter)}",

                            Count = SqlFunc.AggregateCount(s.Id)

                        })

                        .ToList();


    0 回复
  • fate sta fate sta VIP0
    1个月前

    GroupBy(s=>s.CreateTime.ToString("yyyy"))

    这样呢

    0 回复
  • moozy moozy VIP0
    1个月前

    也不行,所有的按照时间分组的我都尝试过了。

    0 回复
  • moozy moozy VIP0
    1个月前

    @fate sta也不行,所有的按照时间分组的我都尝试过了。

    0 回复
  • fate sta fate sta VIP0
    1个月前

    等他们官方回复吧。 我用SQL试了也不行。。

    0 回复
  • fate sta fate sta VIP0
    1个月前

    等他们官方回复吧。 我用SQL试了也不行。。

    0 回复
  • moozy moozy VIP0
    1个月前

    @fate sta:感谢,不大懂,到时候他们官方会在这个帖子下回复吗。

    0 回复
  • fate sta fate sta VIP0
    1个月前
                var list = db.Queryable<Order>()
                .GroupBy("y")
                .Select(it=>new {  y=it.CreateTime.Year })  
               .ToList();


    0 回复
  • fate sta fate sta VIP0
    1个月前

    要这样用。

    0 回复
  • moozy moozy VIP0
    1个月前

    @fate sta:orm生成的sql执行报错,但是如红框改成别名就可以执行成功。image.png

    0 回复
  • moozy moozy VIP0
    1个月前

    @fate sta:我们需要按照年、年月、年月日、年+周、年+季分组统计,如果这种的话应该怎么去实现比较好

    0 回复
  • moozy moozy VIP0
    1个月前

    已经修改源码,支持按照年+周,年+季节分组查询,请合并下。


    0 回复
  • fate sta fate sta VIP0
    1个月前
              var list = db.Queryable<Order>()
                .GroupBy("y")
                .Select(it=>new {  y=it.CreateTime.Year })  
               .ToList();


    这样用就行了。没发现你有pull的代码。

    0 回复
  • moozy moozy VIP0
    1个月前

    GBaseExpressionContext.zip

    你看下,你自己合并下

    0 回复