• Smark.Data 功能强大又灵活的Expression


        Expression对象是Smark.Data的核心对象,虽然Expression是个条件对象,但它所具备的功能是你想象不到的:),以下把Expression的功能列出来。

            public RESULT Avg<RESULT, Entity>(string field) where Entity : Smark.Data.Mappings.DataObject;
            public RESULT Avg<RESULT, Entity>(string field, bool DISTINCT) where Entity : Smark.Data.Mappings.DataObject;
            public RESULT Avg<RESULT, Entity>(string field, IConnectinContext cc) where Entity : Smark.Data.Mappings.DataObject;
            public RESULT Avg<RESULT, Entity>(string field, bool DISTINCT, IConnectinContext cc) where Entity : Smark.Data.Mappings.DataObject;
            public int Count<T>() where T : Smark.Data.Mappings.DataObject;
            public int Count<T>(IConnectinContext cc) where T : Smark.Data.Mappings.DataObject;
            public int Delete<T>() where T : Smark.Data.Mappings.DataObject;
            public int Delete<T>(IConnectinContext cc) where T : Smark.Data.Mappings.DataObject;
            public int Edit<T>(Action<T> handler) where T : Smark.Data.Mappings.DataObject, new();
            public int Edit<T>(params Field[] fields) where T : Smark.Data.Mappings.DataObject, new();
            public int Edit<T>(IConnectinContext cc, Action<T> handler) where T : Smark.Data.Mappings.DataObject, new();
            public int Edit<T>(IConnectinContext cc, params Field[] fields) where T : Smark.Data.Mappings.DataObject, new();
            public IList<T> List<T>() where T : Smark.Data.Mappings.DataObject, new();
            public IList<RESULT> List<T, RESULT>()
                where T : Smark.Data.Mappings.DataObject, new()
                where RESULT : new();
            public IList<RESULT> List<T, RESULT>(params string[] orderby)
                where T : Smark.Data.Mappings.DataObject, new()
                where RESULT : new();
            public IList<T> List<T>(params string[] orderby) where T : Smark.Data.Mappings.DataObject, new();
            public IList<T> List<T>(Region region) where T : Smark.Data.Mappings.DataObject, new();
            public IList<RESULT> List<T, RESULT>(Region region)
                where T : Smark.Data.Mappings.DataObject, new()
                where RESULT : new();
            public IList<RESULT> List<T, RESULT>(IConnectinContext cc, params string[] orderby)
                where T : Smark.Data.Mappings.DataObject, new()
                where RESULT : new();
            public IList<T> List<T>(IConnectinContext cc, params string[] orderby) where T : Smark.Data.Mappings.DataObject, new();
            public IList<RESULT> List<T, RESULT>(IConnectinContext cc, Region region)
                where T : Smark.Data.Mappings.DataObject, new()
                where RESULT : new();
            public IList<T> List<T>(IConnectinContext cc, Region region) where T : Smark.Data.Mappings.DataObject, new();
            public IList<T> List<T>(Region region, params string[] orderby) where T : Smark.Data.Mappings.DataObject, new();
            public IList<RESULT> List<T, RESULT>(Region region, params string[] orderby)
                where T : Smark.Data.Mappings.DataObject, new()
                where RESULT : new();
            public IList<T> List<T>(IConnectinContext cc, Region region, params string[] orderby) where T : Smark.Data.Mappings.DataObject, new();
            public IList<RESULT> List<T, RESULT>(IConnectinContext cc, Region region, params string[] orderby)
                where T : Smark.Data.Mappings.DataObject, new()
                where RESULT : new();
            public T ListFirst<T>() where T : Smark.Data.Mappings.DataObject, new();
            public RESULT ListFirst<T, RESULT>()
                where T : Smark.Data.Mappings.DataObject, new()
                where RESULT : new();
            public RESULT ListFirst<T, RESULT>(IConnectinContext cc)
                where T : Smark.Data.Mappings.DataObject, new()
                where RESULT : new();
            public T ListFirst<T>(IConnectinContext cc) where T : Smark.Data.Mappings.DataObject, new();
            public T ListFirst<T>(params string[] orderby) where T : Smark.Data.Mappings.DataObject, new();
            public RESULT ListFirst<T, RESULT>(params string[] orderby)
                where T : Smark.Data.Mappings.DataObject, new()
                where RESULT : new();
            public T ListFirst<T>(IConnectinContext cc, params string[] orderby) where T : Smark.Data.Mappings.DataObject, new();
            public RESULT ListFirst<T, RESULT>(IConnectinContext cc, params string[] orderby)
                where T : Smark.Data.Mappings.DataObject, new()
                where RESULT : new();
            public RESULT Max<RESULT, Entity>(string field) where Entity : Smark.Data.Mappings.DataObject;
            public RESULT Max<RESULT, Entity>(string field, bool DISTINCT) where Entity : Smark.Data.Mappings.DataObject;
            public RESULT Max<RESULT, Entity>(string field, IConnectinContext cc) where Entity : Smark.Data.Mappings.DataObject;
            public RESULT Max<RESULT, Entity>(string field, bool DISTINCT, IConnectinContext cc) where Entity : Smark.Data.Mappings.DataObject;
            public RESULT Min<RESULT, Entity>(string field) where Entity : Smark.Data.Mappings.DataObject;
            public RESULT Min<RESULT, Entity>(string field, bool DISTINCT) where Entity : Smark.Data.Mappings.DataObject;
            public RESULT Min<RESULT, Entity>(string field, IConnectinContext cc) where Entity : Smark.Data.Mappings.DataObject;
            public RESULT Min<RESULT, Entity>(string field, bool DISTINCT, IConnectinContext cc) where Entity : Smark.Data.Mappings.DataObject;
            public RESULT Sum<RESULT, Entity>(string field) where Entity : Smark.Data.Mappings.DataObject;
            public RESULT Sum<RESULT, Entity>(string field, bool DISTINCT) where Entity : Smark.Data.Mappings.DataObject;
            public RESULT Sum<RESULT, Entity>(string field, IConnectinContext cc) where Entity : Smark.Data.Mappings.DataObject;
            public RESULT Sum<RESULT, Entity>(string field, bool DISTINCT, IConnectinContext cc) where Entity : Smark.Data.Mappings.DataObject;

        以上就是Expression所具备的功能,它的工作可以完成数据查询,统计汇总,修改和删除等操作。当你用Smark.Data进行数据访问的时候相信大部分都是在和这个Expression打交道。下面详细地介绍它的每一个功能。

    数据查询

    当我们new一个Expression出来的时候就可以进行相关操作,只是该操作并不带上条件针对全表操作。

    var employees = exp.List<Employees>();

    获取所有雇员,不过实现查询就没这么简单,有可能加上条件获取某页数据加上分页等。

                    Expression exp = new Expression();
                    if (ProductName != null)
                    {
                        exp &= Modules.Product.productName.Like(ProductName + "%");
                        UrlParams.Add("productname", ProductName);
                    }
                    if (PriceFrom != null)
                    {
                        exp &= Modules.Product.unitPrice >= PriceFrom;
                        UrlParams.Add("pricefrom", PriceFrom.ToString());
                    }
                    if (PriceTo != null)
                    {
                        exp &= Modules.Product.unitPrice <= PriceTo;
                        UrlParams.Add("priceTo", PriceTo.ToString());
                    }
                    DataPage.PageSize = 10;
                    DataPage.RecordCount = exp.Count<Modules.Product>();
                    Records = exp.List<Modules.Product>(new Region(DataPage.PageIndex, DataPage.PageSize),OrderField);

    以上是一个比较常用的查询,根据情况添加相应的查询条件,统计相关条件的记录数并获取对应页的记录数。

    数据删除

    删除一般会执行SQL或在组件中删除对象,但在Smark.Data中Expression可以轻松完成相应的工作。

            Expression exp = new Expression();
            exp.Delete<Employee>();

     删除Employee所有对象,实际情况更多的是基于条件的删除如下:

        (Modules.Employee.city == "gz").Delete<Modules.Employee>();

    这样我们就可以把某个城市的employee删除了

    数据更新

        Expression exp = new Expression();
        exp.Edit<Modules.Employee>(o => { o.City = "bbq"; });
        (Modules.Employee.city == "gz").Edit<Modules.Employee>(o => { o.City = "bbq"; });

    基于同样的方式就能进行数据编辑

    Expression能处理怎样的条件?

    Expression所提供的条件组合非常灵活,重载了|和&使条件编写可以更好地接近实际SQL的方式,对于一此运算的多样性也利用得很好.

     qual = (Qualification.sellerID == qinfo.SellerID & Qualification.storeID == qinfo.StoreID).ListFirst<Qualification>();
                    Expression exp = new Expression();
                    if (EmployeeID != null)
                        exp &= Modules.Order.employeeID.At() == EmployeeID;
                    if (CustomerID != null)
                        exp &= Modules.Order.customerID.At() == CustomerID;
                    if (OrderDateFrom != null)
                        exp &= Modules.Order.orderDate >= OrderDateFrom;
                    if (OrderDateTo != null)
                        exp &= Modules.Order.orderDate <= OrderDateTo;
                    if (RequiredDateFrom != null)
                        exp &= Modules.Order.requiredDate >= RequiredDateFrom;
                    if (RequiredDateTo != null)
                        exp &= Modules.Order.requiredDate <= RequiredDateTo;
       (Modules.Employee.employeeID == new int[] {2,4,5 }).Delete<Modules.Employee>();
       (Modules.Employee.employeeID != new int[] {2,4,5 }).Delete<Modules.Employee>();
       Modules.Order.orderDate["1997-1-1", "1997-2-1"].List<Modules.Order>();
    (Modules.Order.employeeID== Modules.Employee.employeeID[Modules.Employee.city==new []{"gz","sz"}]).List<Modules.Order>();

    Expression对象是非常灵活,但它的作用建是立在实体描述<T>上,Smark.Data的实体描述也非常灵活除了描述表外,还能描述关联查询,汇总统计结合Expression对象可以满足大部分实际应用的需要。在下一章节会讲解实体对象的描述。

    Smark.Data是基于 Apache License 2.0 (Apache)协议的开源组件,详情可以到http://www.ikende.com/了解

    访问Beetlex的Github
  • 相关阅读:
    数码管模块
    iis报mmc检测错误解决办法
    图片显示加时间戳
    C#.Net上传文件大小限制设置
    DoNet 打包,能够自动生成数据库(可以执行某些exe,vbs文件)
    aspx模式窗口
    aspx页面不能及时更新数据
    aspx 页面提交造成页面样式混乱
    MSSQL中返回刚插入记录的ID
    修改SQL数据库中表字段类型时,报“一个或多个对象访问此列”错误的解决方法
  • 原文地址:https://www.cnblogs.com/smark/p/2630844.html
Copyright © 2020-2023  润新知