• crm高速开发之QueryExpression


    /* 创建者:菜刀居士的博客
     * 创建日期:2014年07月06号
     */

    namespace Net.CRM.OrganizationService
    {
        using System;
        using Microsoft.Xrm.Sdk;
        using Microsoft.Xrm.Sdk.Query;
        using System.Collections.Generic;

        /// <summary>
        /// QueryExpression
        /// </summary>
        public class QueryExpressionDemo
        {
            /// <summary>
            /// 基本模式,构造查询--QueryExpression
            /// </summary>
            public void CreateQueryExpression()
            {
                QueryExpression query = new QueryExpression();
                //查询的实体名称
                query.EntityName = "new_accountproduct";
                //查询实体的属性集合
                query.ColumnSet = new ColumnSet("new_product","new_name","new_price");
                //查询条件
                query.Criteria.AddCondition(new ConditionExpression("statecode",ConditionOperator.Equal,0));
                query.Criteria.AddCondition(new ConditionExpression("new_name", ConditionOperator.Like, "水果" + "%"));
                List<Guid> list = new List<Guid>();
                list.Add(new Guid("27BB1B31-09BB-E311-B590-D89D671782D3"));
                list.Add(new Guid("E8F8D412-31BB-E311-B590-D89D671782D3"));
                query.Criteria.AddCondition(new ConditionExpression("new_product",ConditionOperator.In,list.ToArray()));
                query.Criteria.AddCondition(new ConditionExpression("new_name", ConditionOperator.NotNull));
                query.Criteria.AddCondition(new ConditionExpression("new_price", ConditionOperator.Null));
            }

            /// <summary>
            /// 高速模式,构造查询--QueryExpression
            /// </summary>
            public void CreateQuickQueryExpression()
            {
                QueryExpression query = "new_accountproduct".ToQuery("new_product", "new_name", "new_price");
                query.AddEqual("statecode", 0);
                query.AddLike("new_name", "水果" + "%");
                List<Guid> list = new List<Guid>();
                list.Add(new Guid("27BB1B31-09BB-E311-B590-D89D671782D3"));
                list.Add(new Guid("E8F8D412-31BB-E311-B590-D89D671782D3"));
                query.AddIn("new_product", list.ToArray());
                query.AddNotNull("new_name");
                query.AddNull("new_price");
            }


        }

        public static class ExtensionFunction
        {
            public static QueryExpression ToQuery(this string entityName)
            {
                QueryExpression query = new QueryExpression();
                query.EntityName = entityName;
                return query;
            }

            public static QueryExpression ToQuery(this string entityName,params string[] Attrs)
            {
                QueryExpression query = new QueryExpression();
                query.EntityName = entityName;
                query.ColumnSet = new ColumnSet(Attrs);
                return query;
            }

            public static void AddEqual<T>(this QueryExpression query,string name,T value)
            {
                query.Criteria.AddCondition(new ConditionExpression(name,ConditionOperator.Equal,value));
            }

            public static void AddLike(this QueryExpression query, string name,string value)
            {
                query.Criteria.AddCondition(new ConditionExpression(name,ConditionOperator.Like,value));
            }

            public static void AddIn<T>(this QueryExpression query, string name,T[] values)
            {
                query.Criteria.AddCondition(new ConditionExpression(name, ConditionOperator.In, values));
            }

            public static void AddNotNull(this QueryExpression query,params string[] attrs)
            {
                if (attrs != null && attrs.Length > 0)
                {
                    foreach(string name in attrs)
                    {
                        query.Criteria.AddCondition(new ConditionExpression(name, ConditionOperator.NotNull));
                    }
                }
            }

            public static void AddNull(this QueryExpression query, params string[] attrs)
            {
                if (attrs != null && attrs.Length > 0)
                {
                    foreach (string name in attrs)
                    {
                        query.Criteria.AddCondition(new ConditionExpression(name, ConditionOperator.Null));
                    }
                }
            }
        }


    }

    对照:

  • 相关阅读:
    在SQL SERVER 2005中还原数据库时出现错误:system.data.sqlclient.sqlerror 媒体集有 2 个媒体簇 但只提供了 1 个。必须提供所有成员。 (microsoft.sqlserver.smo)
    Mysql的Root密码忘记,查看或修改的解决方法
    Win7系统如何设置FTP详细过程
    该设备或资源(127.0.0.1)未设置为接受端口“16823”上的连接。
    window7防火墙无法更改某些设置,错误代码0×80070422
    访问FTP站点下载文件,提示“当前的安全设置不允许从该位置下载文件”的解决方案
    解决SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问的方法
    按状态选择链接元素
    类选择器与ID选择器的比较
    关于创建Web图像时应记住的五个要素
  • 原文地址:https://www.cnblogs.com/blfshiye/p/5105105.html
Copyright © 2020-2023  润新知